如何在VBA中使用.Find的多个条件?

时间:2013-08-02 14:15:42

标签: vba ms-access access-vba dao

我正在尝试.FindLast搜索特定记录,并且它正在使用一个标准,但是当我尝试使用.FindLast时有多个条件它停止工作。但是,我在.FindFirst中使用了几乎相同的语句,这就是我感到困惑的原因。

我得到的错误是“条件表达式中的数据类型不匹配”。错误是这一行:rst.FindLast(“DONOR_CONTACT_ID ='strDonor1'和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定义为Decimal,精度为11,RECIPIENT_CONTACT_ID定义为Decimal,精度为11,ORDER_NUMBER定义为Decimal,精度为15。然后我将代码中的变量定义为变体。你认为这可能有问题吗?

2 个答案:

答案 0 :(得分:4)

我认为如果你改变这一点,你的解决问题的努力会更容易......

rst.FindLast ("DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'")

这样的事情......

Dim strFind As String
strFind = "DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'"
Debug.Print strFind
rst.FindLast strFind

当代码抛出错误,或者根本找不到您期望的内容时,请转到立即窗口( Ctrl + g )并检查{的输出{1}}。您可以立即发现问题。如果没有,请复制Debug.Print strFind输出,在查询设计器中打开新查询,切换到SQL视图并在Debug.Print子句中使用复制的文本。在这种情况下,我认为查询SQL可能是:

WHERE

yadda_yadda 替换为您从“立即”窗口复制的文本。

这更像是一般的故障排除建议。对于此特定问题,我认为您正在构建SELECT * FROM T_RECIPIENT_SORT WHERE yadda_yadda; 文本以包含变量的名称而不是那些变量'。当Find这两个字符串表达式时,看看你得到了什么。

Debug.Print

你的代码使用了第一个,但我认为你实际上需要第二个。

在您的问题更新中,您报告"DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'" "DONOR_CONTACT_ID= '" & strDonor1 & "' AND ORDER_NUMBER= '" & strOrderNum1 & "'" DONOR_CONTACT_ID都是数字数据类型。在这种情况下,请勿在{{1​​}}字符串中引用这些搜索值。

ORDER_NUMBER

答案 1 :(得分:0)

我们可能有一些缺少的数据,其中DONOR_CONTACT_ID匹配,但ORDER_NUMBER是否为空?我认为Access会抛出你从那种情况中得到的那种错误。

除非第一次出现是罪魁祸首,否则不会发生在FindFirst上。