0

我有以下简单易行的代码:

Private Sub Add_Click()
Dim db As Database, rsAtype As Recordset, Criteria As String

    Set db = CurrentDb
    Set rsAtype = db.OpenRecordset("Asset_Types", DB_OPEN_DYNASET)

Criteria = "Type='" & NOA & "'"

rsAtype.FindFirst Criteria

'****  Following code is Adding a new type of asset to the Asset_Types Table****
If rsAtype.NoMatch Then

        rsAtype.AddNew
        rsAtype("Type") = Me!NOA
        rsAtype("Description") = Me!Desc
        rsAtype.Update
MsgBox "New Asset Added"
    rsAtype.Close
    db.Close
    DoCmd.Close
Else
        MsgBox "Asset Type " & Me!NOA & " already exists.", 48, "ERROR!"
        Me!NOA.SetFocus

End If

End Sub

在这里,我正在搜索资产类型是否已经存在,然后发出警告而不是更新,有什么方法可以只使用一个 If 语句来搜索表中的多个列,我不想创建嵌套的 If 语句。

4

2 回答 2

0

好吧,我找到了答案,AND 必须是引号的一部分:

旧代码

Criteria = "Supplier_name='" & Me!Supnamebox & "'" And "Contact_name='" & Me!ContPerbox & "'" And "Contact_number='" & Me!Me!Phonebox & "'" And "Contact_email='" & Me!emailbox & "'"

新代码

Criteria = "Supplier_name='" & Me!Supnamebox & "' AND Contact_name='" & Me!ContPerbox & "' AND Contact_number='" & Me!Phonebox & "' AND Contact_email='" & Me!emailbox & "'"

我已经对其进行了测试并且它可以工作,但建议的话,语法是正确的,但我使用的逻辑是错误的,我应该使用 OR 而不是 AND。OR 正在测试是否有任何匹配的字段,如果所有字段都匹配,AND 只会给出错误,否则它会让您添加重复记录。

于 2013-05-29T23:56:38.117 回答
0

是的,只需结合多个标准。例如:

strCriteria = "[PlantID]=" & Me!PlantID & " AND [Location]= '" & Me!Location & "'"

在这里,第一个参数PlantID是一个整数,第二个参数是Location一个字符串(因此是单引号)。

于 2013-05-29T01:26:03.517 回答