Check to see whether or not the record exist before close

H

hngo

Hi,

Is there away that we can check to see whether or not the record is exist in
a database (the PK is MyID). IF the record exist ask before save. If new
record save the record.

Please help, many thanks in advance
 
B

Brian

If Form.NewRecord Then...Else...End If

However, if you are using bound controls, Access automatically saves the
record (whether new or changed) upon closing the form or navigating to a new
record, so you may want to set this up to give the user an option to cancel.

If Not Form.NewRecord and Me.Dirty Then
If Not MsgBox("Do you wish to save your changes?", vbExclamation + vbYesNo,
"Update?") = vbYes Then Me.Undo
End If
 
H

hngo

Hi Brian,

new record is save over the existing data. New record has not been add to
the table. What have I done wrong? (the code has been enter in the after
update event)
 
B

Brian

AfterUpdate doesn't happen until after the record is saved. Move it to
Form_BeforeUpdate & Cancel/Undo if the user answers No:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not Form.NewRecord and Me.Dirty Then
If MsgBox("Do you wish to save your changes?", vbExclamation + vbYesNo,
"Save changes?") = vbNo Then
Cancel = True
Me.Undo
End If
End If
End Sub
 
Top