我想.FindLast来搜索特定的记录,并且它是一个标准的工作,但是当我试图用多个条件使用.FindLast它停止工作。 但是,我使用几乎相同的语句与.FindFirst和它的作品这就是为什么我很困惑。
我得到的错误是“标准表达式中数据类型不匹配”。 和错误是这一行:rst.FindLast( “DONOR_CONTACT_ID = 'strDonor1' AND ORDER_NUMBER = 'strOrderNum1'”)。 我通过代码和行.FindFirst(“DONOR_CONTACT_ID =‘strDonor1’和ORDER_NUMBER =‘strOrderNum1’”)但是正确的工作加强。
Option Compare Database
Option Explicit
Public dbs As DAO.Database
Public rst As DAO.Recordset
Public rstOutput As DAO.Recordset
'Defines DAO objects
Public strDonor1 As Variant
Public strDonor2 As Variant
Public strRecip1 As Variant
Public strRecip2 As Variant
Public strOrderNum1 As Variant
Public strOrderNum2 As Variant
Public strLastDonor As Variant
Function UsingTemps()
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("T_RECIPIENT_SORT", dbOpenDynaset)
'rst refers to the table T_RECIPIENT_SORT
Set rstOutput = dbs.OpenRecordset("T_OUTPUT", dbOpenDynaset)
'rstOutput refers to the table T_OUTPUT
rst.MoveFirst
'first record
strDonor1 = rst!DONOR_CONTACT_ID
'sets strTemp1 to the first record of the DONOR_CONTACT_ID
strRecip1 = rst!RECIPIENT_CONTACT_ID
strOrderNum1 = rst!ORDER_NUMBER
rst.MoveNext
'moves to the next record
Do While Not rst.EOF
'Loop while it's not the end of the file
strDonor2 = rst!DONOR_CONTACT_ID
'strTemp2 = DONOR_CONTACT_ID from T_RECIPIENT_SORT
strRecip2 = rst!RECIPIENT_CONTACT_ID
strOrderNum2 = rst!ORDER_NUMBER
'Sets strRecip = RECIPIENT_CONTACT_ID FROM T_RECIPIENT_SORT
With rstOutput
'Uses T_OUTPUT table
If (strDonor1 = strDonor2) And (strOrderNum1 = strOrderNum2) Then
'Runs if temps have same DONOR_CONTACT ID
If .RecordCount > 0 Then
'If table has records then you can check
rst.FindLast ("DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'")
strLastDonor = rst!RECIPIENT_CONTACT_ID
If strLastDonor = strRecip2 Then
Call LastDonor
Else
Call FirstDonor
End If
Else
'No records in T_Output so needs to add first record
.AddNew
!DONOR_CONTACT_ID = strDonor1
!RECIPIENT_1 = strRecip1
!ORDER_NUMBER = strOrderNum1
.Update
End If
Else
.FindFirst ("DONOR_CONTACT_ID= 'strDonor1' and ORDER_NUMBER= 'strOrderNum1'")
If .NoMatch Then
.AddNew
!DONOR_CONTACT_ID = strDonor1
!RECIPIENT_1 = strRecip1
!ORDER_NUMBER = strOrderNum1
.Update
End If
End If
End With
'Slides variables down
rst.FindFirst "[RECIPIENT_CONTACT_ID] = " & strRecip2
strDonor1 = strDonor2
strRecip1 = strRecip2
strOrderNum1 = strOrderNum2
rst.MoveNext
Loop
Call LastRecord
Set dbs = Nothing
Set rst = Nothing
Set rstOutput = Nothing
End Function
编辑:
我刚添加以下代码:
Dim strFind As Variant
strFind = "DONOR_CONTACT_ID= '" & strDonor1 & "' AND ORDER_NUMBER= '" & strOrderNum1 & "'"
Debug.Print strFind
rst.FindLast strFind
它显示此与Debug.Print:
DONOR_CONTACT_ID= '10136851341' AND ORDER_NUMBER= '112103071441001'
这些是DONOR_CONTACT_ID和ORDER_NUMBER正确的价值观,但我得到的错误“条件表达式中数据类型不匹配”与线rst.FindLast strFind。 难道可能是因为我定义我的变量变异? 在该表中我已经DONOR_CONTACT_ID定义为十进制与11的精度,定义为具有11精度小数RECIPIENT_CONTACT_ID,和ORDER_NUMBER与15精度小数。 然后,我在我的代码定义变量变异。 你认为有可能是一个问题吗?