Save Records?

german

Registered User.
Local time
Yesterday, 21:00
Joined
Jul 5, 2012
Messages
21
How to write vba code to save the details after checking for duplication.

in a form


Thanks in advance
 
When you close the form it automatically commits all changes.. if you wish to close using VB code then use..
Code:
DoCmd.Close acForm, Me.Name
 
What are you checking for duplication? A combination of fields?

You could use the form's BeforeUpdate event.

Code:
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim lngNoRecords as Long, strSql as string

'This checks for duplicates before saving a new record
  If me.NewRecord then 

'   In this example Field1 is a string, Field2 is a number  
    strSql = "Field1 = '" & Control1 & "' AND Field2 = " & Control2

'   lngNoRecords should return 0 if no matching record exists or the number of matching records
    lngNoRecords = DCount("*","yourTableName",strSql)

'   Cancels update of record if one already exists
    If lngNoRecords > 0 then
      Cancel = TRUE
      MsgBox "A record already exists"
    End If

  End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom