Vba 如何将rst.FindFirst与rst.NoMatch一起使用?

Vba 如何将rst.FindFirst与rst.NoMatch一起使用?,vba,ms-access,dao,Vba,Ms Access,Dao,除了这一行之外,我的代码还能工作 .FindFirst "[DONOR_CONTACT_ID] = strTemp2" 我希望我的代码检查是否存在一个记录,其中存在一个特定的捐赠者联系人ID,因为存在多个具有相同捐赠者联系人ID的记录。如果该记录不存在,则我希望将该捐赠者联系人ID和收受者联系人ID添加到收件人1。如果该记录确实存在,我想将收件人\u联系人\u ID添加到该特定捐赠者\u联系人\u ID的收件人\u 2中。为此,我先使用.FindFirst查看是否存在记录,然后使用.NoMat

除了这一行之外,我的代码还能工作

.FindFirst "[DONOR_CONTACT_ID] = strTemp2"
我希望我的代码检查是否存在一个记录,其中存在一个特定的捐赠者联系人ID,因为存在多个具有相同捐赠者联系人ID的记录。如果该记录不存在,则我希望将该捐赠者联系人ID和收受者联系人ID添加到收件人1。如果该记录确实存在,我想将收件人\u联系人\u ID添加到该特定捐赠者\u联系人\u ID的收件人\u 2中。为此,我先使用.FindFirst查看是否存在记录,然后使用.NoMatch。如果没有匹配项,我想添加一条新记录,但如果有,我想检查它是否必须进入收件人2中

我得到的错误是“无法将'strTemp2'识别为有效的字段名或表达式”。我想看看记录是否等于strTemp2,但我认为我的语法是错误的。谢谢你的帮助

这是我的密码:

Option Compare Database
Option Explicit

Function UsingTemps()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim rstOutput As DAO.Recordset
'Defines DAO objects
Dim strTemp1 As String
Dim strTemp2 As String
Dim strVal As String
Dim strRecip As String

DoCmd.SetWarnings False
DoCmd.OpenQuery ("Q_RECIPIENT_SORT")
DoCmd.OpenQuery ("Q_DELETE_T_OUTPUT")
DoCmd.SetWarnings True
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
strTemp1 = rst!DONOR_CONTACT_ID
'sets strTemp1 to the first record of the DONOR_CONTACT_ID
rst.MoveNext
'moves to the next record


    Do While Not rst.EOF
    'Loop while it's not the end of the file
        strTemp2 = rst!DONOR_CONTACT_ID
        'strTemp2 = DONOR_CONTACT_ID from T_RECIPIENT_SORT

    If strTemp1 = strTemp2 Then
    'Runs if temps have same DONOR_CONTACT ID
        strRecip = rst!RECIPIENT_CONTACT_ID
    'Sets strRecip = RECIPIENT_CONTACT_ID FROM T_RECIPIENT_SORT

        With rstOutput
        'Uses T_OUTPUT table
            If .RecordCount > 0 Then
            'If table has records then you can check
                .FindFirst "[DONOR_CONTACT_ID] = strTemp2"
                If .NoMatch Then
                    .AddNew
                    !DONOR_CONTACT_ID = strTemp1
                    !RECIPIENT_1 = strRecip
                    .Update
                Else

                    If !DONOR_CONTACT_ID = strTemp2 Then
                        If IsNull(!RECIPIENT_2) And Not (IsNull(!RECIPIENT_1)) Then
                            .Edit
                            !RECIPIENT_2 = strRecip
                            .Update
                        End If
                        .AddNew
                        !DONOR_CONTACT_ID = strTemp2
                        !RECIPIENT_1 = strRecip
                        .Update
                    End If
                End If

            Else
                .AddNew
                !DONOR_CONTACT_ID = strTemp2
                !RECIPIENT_1 = strRecip
                .Update
            End If

        End With

    End If

    strTemp1 = strTemp2
    rst.MoveNext

Loop

Set dbs = Nothing

End Function
将变量的值而不是变量名构建到给定的字符串中以查找

假设
捐赠者联系人ID
是文本数据类型,还包括变量值周围的引号

.FindFirst“[捐赠者\联系人\ ID]=”&strTemp2&“
但是如果是数字,你不需要那些引号

.FindFirst“[捐赠者联系人ID]=”&strTemp2

您连续第二天帮助了我!再次感谢!我办公室里没有人知道如何编码,所以我真的很感激你在过去的两天里帮助了我,节省了我这么多时间!