0

我想检查我的EMP表中的字段是否有重复项。一切正常,除了当字段中没有任何内容时我需要创建一个异常,即异常。

Private Sub cmdDuplicates2_Click() 
Dim Name As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("EMP", dbOpenDynaset)
Name = Me.Personnel
rst.FindFirst "[Personnel] = '" & Name & "'"
If rst.NoMatch Then
        MsgBox "No duplicates found"
  Else
        MsgBox "Name is already in Database"
End If

End Sub

编辑:现在就这样做。如果为空,则显示“请输入名称”。但它也显示“未找到重复项”。我希望它只说“请输入名称”。如果该字段为空

Private Sub cmdDuplicates2_Click()
Dim Name As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("EMP", dbOpenDynaset)

If IsNull(Me.Personnel) Then MsgBox "Please enter a name." Else: Name = Me.Personnel

rst.FindFirst "[Personnel] = '" & Name & "'"
If rst.NoMatch Then
        MsgBox "No duplicates found"
  Else
        MsgBox "Name is already in Database"
End If

End Sub
4

2 回答 2

1

做这样的事情:

If IsNull(Me.Personnel) Then Name = "Empty" Else Name = Me.Personnel
于 2013-09-04T16:53:53.110 回答
0

尝试这个。在这个版本中,我实际上是在计算每个 Personnel 值的记录数:

Private Sub cmdDuplicates2_Click() 
Dim rst As DAO.Recordset
Dim Qry as string
Qry="Select count(*) from EMP where [Personnel]='" & Me.Personnel & "'"

Set rst = Currentdb.OpenRecordset(Qry, dbOpenDynaset)

If rst.fields(0)>1 then
        MsgBox "Name is already in Database"
  Else
        MsgBox "No duplicates found"
End If

End Sub

编辑

抱歉,我的第一直觉是按照我的方式重构您的代码。尝试将此添加到程序的开头:

If Me.Personnel="" then 
    msgbox "Please enter a name."
    exit sub
end if
于 2013-09-04T14:32:16.503 回答