vba 在创建新记录之前检查重复记录的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19379981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:50:00  来源:igfitidea点击:

check a record for Duplicates Records, Before Creating New Records

vbaaccess-vba

提问by Janeyg

I am new to this site and new to vba code. Another user posted this question and the answer code seemed to be the code I was looking for. I want to check several fields for duplicate data, flag a message and go to the record. I was hoping this code would do that. Except when I add my own fields to check I get error code 3077 - syntax error and I am not sure how to fix it. any help would be appreciated. The line highted for this error is the 3rd line down. I am using access 2010. I am not sure how to link to the question but was advised I should ask a new question regarding my problem.

我是这个网站的新手,也是 vba 代码的新手。另一个用户发布了这个问题,答案代码似乎是我正在寻找的代码。我想检查多个字段是否有重复数据,标记一条消息并转到记录。我希望这段代码能做到这一点。除非我添加自己的字段来检查我收到错误代码 3077 - 语法错误,我不知道如何修复它。任何帮助,将不胜感激。此错误的高线是第 3 行。我正在使用 access 2010。我不确定如何链接到该问题,但有人建议我应该就我的问题提出一个新问题。

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim rst As Recordset
    Set rst = Me.RecordsetClone
    rst.FindFirst "[ID] <> " & Me.ID & " AND [TitleText] = " & Me.TitleText & " AND [UnitCode] = " & Me.UnitCode & " AND  [AcademicYear] = " & Me.AcademicYear & " AND    [Titleofchapterjournalarticle] = " & Me.Titleofchapterjournalarticle
    If Not rst.NoMatch Then
        Cancel = True
        If MsgBox("A record matching these fields already exist", vbYesNo) = vbYes Then
            Me.Undo
            DoCmd.SearchForRecord , , acFirst, "[ID] = " & rst("ID")
        End If
    End If
    rst.Close
End Sub

回答by usncahill

I think you mean that the fourth line down is the one highlighted, rst.FindFirst....

我想你的意思是第四行是突出显示的,rst.FindFirst....

If that is the case, there is a syntax error in your FindFirststring. It could be that your TitleTextor Titleofchapterjournalarticlefields contained spaces or logic code (OR, AND). To prevent this, surround any string type fields with ' (single quote) inside the string.

如果是这种情况,则FindFirst字符串中存在语法错误。可能是您的 TitleTextTitleofchapterjournalarticle字段包含空格或逻辑代码(OR、AND)。为防止出现这种情况,请在字符串内用 '(单引号)将任何字符串类型字段括起来。

The final result will look like:

最终结果将如下所示:

rst.FindFirst "[ID] <> " & Me.ID & _
 " AND [TitleText] = '" & Me.TitleText & _
 "' AND [UnitCode] = " & Me.UnitCode & _
 " AND [AcademicYear] = " & Me.AcademicYear & _
 " AND [Titleofchapterjournalarticle] = '" & Me.Titleofchapterjournalarticle & "'"