我有以下简单易用的代码:
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语句。
答案 0 :(得分:0)
是的,只需合并多个标准即可。例如:
strCriteria = "[PlantID]=" & Me!PlantID & " AND [Location]= '" & Me!Location & "'"
这里,第一个参数PlantID
是一个整数,第二个参数Location
是一个字符串(因此是单引号)。
答案 1 :(得分: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正在测试是否有匹配的字段,如果所有字段都匹配,则只会出错,否则会让您添加重复记录。