2015-05-14 107 views
0

如何防止重複值不插入表中。我創建了一個INSERT,UPDATE和DELETE的代碼,我想顯示一個MsgBox,它有一個重複的值並取消它。謝謝。下面你的代碼:訪問VBA /防止重複值

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 
+1

非常簡單:當一個新值插入時,首先儘量選擇它。如果選擇的結果不爲空,則表示它是重複的,並且不允許其餘代碼運行。注意:如果您的數據可能存在風險,您可能會遇到[SQL注入](http://www.w3schools.com/sql/sql_injection.asp),請考慮使用參數化SQL(我不知道AdoDB的VBA提供雖然,也許嘗試清除插入自己,如果沒有)。 –

+2

您可以首先查詢數據庫,以查看其他記錄是否已與您要插入/更新的內容匹配。或者在表中添加約束條件,不允許在定義它們時使用重複項。 –

回答

0

您可以更改您的SQL以使用IF EXISTS條件並僅在記錄尚不存在時插入。

你的SQL可能看起來像:

IF NOT EXISTS ( SELECT ...... )

BEGIN 
     INSERT INTO tblEmployees ......<insert since employee does not exists> 
    END 
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字符串。

+2

我剛剛發佈相同的解決方案。讓OP知道'emiD'字段的更新是無用的,因爲這是更新所基於的字段。 – Jeeped

+0

謝謝,我沒有仔細關注查詢的內容,也沒有注意到這一點。 –