首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rst.NoMatch中使用rst.FindFirst?

如何在rst.NoMatch中使用rst.FindFirst?
EN

Stack Overflow用户
提问于 2013-07-12 21:29:10
回答 1查看 37K关注 0票数 6

我的代码可以正常工作,除了这一行

代码语言:javascript
复制
.FindFirst "[DONOR_CONTACT_ID] = strTemp2"

我希望我的代码检查是否存在记录,其中存在特定的DONOR_CONTACT_ID,因为有多个记录具有相同的DONOR_CONTACT_ID。如果该记录不存在,则我希望将该DONOR_CONTACT_ID和RECIPIENT_CONTACT_ID添加到RECIPIENT_1。如果该记录存在,我希望将该RECIPIENT_CONTACT_ID添加到该特定DONOR_CONTACT_ID的RECIPIENT_2。为此,我使用.FindFirst,以查看是否存在记录,然后使用.NoMatch。如果没有匹配项,我想添加一个新记录,但如果有,我想检查它是否必须放在RECIPIENT_2中。

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

下面是我的代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-12 21:50:48

将变量的值而不是变量名构建到FindFirst要查找的字符串中。

假设DONOR_CONTACT_ID是text数据类型,还要在变量的值两边加上引号...

代码语言:javascript
复制
.FindFirst "[DONOR_CONTACT_ID] = '" & strTemp2 & "'"

但是如果它是数字的,你不需要那些引号...

代码语言:javascript
复制
.FindFirst "[DONOR_CONTACT_ID] = " & strTemp2
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17616069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档