访问VBA /防止重复值

时间:2015-05-14 14:17:21

标签: vba access-vba

如何防止重复值不插入表中。我已经创建了一个INSERT,UPDATE和DELETE的代码,我想显示一个有重复值的MsgBox并取消它。谢谢。下面是代码:

Private Sub Command12_Click()

    If Me.emID.Tag & "" = "" Then

        If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
            Me.emID.BorderColor = vbRed
            Me.emFirst.BorderColor = vbRed
            Me.emLast.BorderColor = vbRed
            MsgBox "Please fill required fields", vbInformation, "Information"
            Exit Sub
        End If


            CurrentDb.Execute "INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment)" & _
            "VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "')"
            MsgBox "Record Added", vbInformation, "information"

            Else
                CurrentDb.Execute "UPDATE tblEmployees " & _
                "SET emiD =" & Me.emID & _
                ", first ='" & Me.emFirst & "'" & _
                ", last = '" & Me.emLast & "'" & _
                ", gender ='" & Me.emGender & "'" & _
                ", phone = '" & Me.emPhone & "'" & _
                ", mobphone ='" & Me.emMob & "'" & _
                ", city ='" & Me.emCity & "'" & _
                ", state ='" & Me.emState & "'" & _
                ", zip ='" & Me.emZip & "'" & _
                ", adress ='" & Me.emAdress & "'" & _
                ", email ='" & Me.emEmail & "'" & _
                ", comment ='" & Me.emComment & "'" & _
                "WHERE emID =" & Me.emID.Tag
                MsgBox "Updated!", vbInformation, "Information"
    End If

Me.tblEmployees_subform.Form.Requery

End Sub

2 个答案:

答案 0 :(得分:1)

听起来您想要更新员工,如果存在给定ID,否则您想要添加新员工。您可以通过首先尝试使用给定ID更新员工记录来防止添加重复员工,如果没有更新记录,那么您是否添加新员工记录。

Private Sub Command12_Click()
    If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
        Me.emID.BorderColor = vbRed
        Me.emFirst.BorderColor = vbRed
        Me.emLast.BorderColor = vbRed
        MsgBox "Please fill required fields", vbInformation, "Information"
        Exit Sub
    End If

    ' You must set CurrentDb to a variable otherwise the RecordsAffected
    ' property used later will be incorrect.
    Dim db As DAO.Database
    Set db = CurrentDb

    ' First try to update an existing employee.
    db.Execute _
        "UPDATE tblEmployees " & _
        "SET first ='" & Me.emFirst & "', " & _
            "last = '" & Me.emLast & "', " & _
            "gender ='" & Me.emGender & "', " & _
            "phone = '" & Me.emPhone & "', " & _
            "mobphone ='" & Me.emMob & "', " & _
            "city ='" & Me.emCity & "', " & _
            "state ='" & Me.emState & "', " & _
            "zip ='" & Me.emZip & "', " & _
            "adress ='" & Me.emAdress & "', " & _
            "email ='" & Me.emEmail & "', " & _
            "comment ='" & Me.emComment & "'" & _
        "WHERE emID =" & Me.emID.Tag & ";"

    ' If no records were affected by update then add a new employee.
    If db.RecordsAffected = 0 Then
        db.Execute _
            "INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment) " & _
            "VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "');"
        MsgBox "Record Added", vbInformation, "Information"
    Else
        MsgBox "Updated!", vbInformation, "Information"
    End If

    Me.tblEmployees_subform.Form.Requery
End Sub

注意:在更新查询中,我删除了对emID字段的更新,因为这是查询所基于的(在WHERE子句中)。如果emID字段正在更改,您将无法使用新的emID值来查找具有旧emID值的员工记录。

如果您从不想要任何重复项,我还建议您在数据库表中添加约束以防止重复,如Daniel Cook所建议的那样。我还建议使用参数化查询,而不是在VBA中构建SQL字符串。

答案 1 :(得分:0)

您可以更改SQL以使用IF EXISTS条件,并仅在记录尚不存在时插入。

您的SQL可能如下所示:

如果不存在         (         选择 ......         )

    BEGIN
        INSERT INTO tblEmployees ......<insert since employee does not exists>
    END