Question : Access Checking for duplicates , Help with "You can't save this record at this time"

Hi,
I have a form that adds a new rrecord to the db i want to check if the record exists before the update, i have the following code that when i ran i get the "you cn't sav this record at this time" any clues? below my code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim lID
Dim sWhere As String
DoCmd.SetWarnings False
If save_source <> "saveb" Then
   If MsgBox("Do you want to save your changes?", vbYesNo, "Save Changes") = vbNo Then
      Cancel = True
   Else
      sWhere = UCase(Replace(Me.cli_name & Mid(Me.cli_address1, 1, 10) & Mid(Me.cli_zip, 1, 5), " ", ""))
      lID = DLookup("[client_id]", "client", "[cli_prevent_dup]= '" & sWhere & "'")
      If IsNull(lID) = False Then
         MsgBox " este registro ya existe"
         Cancel = True
      End If
   End If
End If
DoCmd.SetWarnings True
End Sub

Answer : Access Checking for duplicates , Help with "You can't save this record at this time"

Code seems fine, chould be a bit shorter,

Create a new sub and call it from each feild after update, remove code from before update.

Private Sub CheckForDuplicates()
Dim lID
Dim sWhere As String
sWhere = UCase(Replace(Me.cli_name & Mid(Me.cli_address1, 1, 10) & Mid(Me.cli_zip, 1, 5), " ", ""))

If sWhere = DLookup("[client_id]", "client", "[cli_prevent_dup]= '" & sWhere & "'") Then
         MsgBox "There is already with these details?"
         Cancel = True
End If
End Sub

then in the after update for each of the 3 feilds add
Call CheckForDuplicates

this should work fine and checks way before you go and save.
Random Solutions  
 
programming4us programming4us