Help with code to confirm changes

R

Randy

I found this code from an access website. It's supposed to give a warning
only if there is a change to a previously entered record. It does work,
except it also warns when a new record is entered. I dont want it to warn
on a new record. Ive tried this code in the before update, on change etc,
but cant get it to do what I want...Any ideas? Thanks

Private Sub CboAppID_Change()
If MsgBox("Changes have been made to the Applicant ID" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
 
D

Douglas J. Steele

Try putting it in the form's BeforeUpdate event, rather than an event
associated with the combobox.
 
R

Randy

I placed the code in the before update of the form. The same thing happens
as before. The message box pops up even on a new record..Thanks...Randy
 
T

tina

first, i wouldn't use the control's Change event, because it fires on every
keystroke in the control. i'd use the control's BeforeUpdate event, and
check to see if the record is a new record, as

Private Sub CboAppID_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
If MsgBox("Changes have been made to the Applicant ID" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
Cancel = True
Me!CboAppID.Undo
End If
End If
End Sub

note that DoCmd.Save will save the *form object*, not the current record or
the current control. so i changed that line of code. also, you probably
don't need to explicitly save the record when the update occurs, unless
something else is going on that mandates an immediate write to the table. i
would probably use

Private Sub CboAppID_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
If MsgBox("Changes have been made to the Applicant ID" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbNo Then
Cancel = True
Me!CboAppID.Undo
End If
End If
End Sub

btw, you might also want to force your user to think before answering the
message box (most people get in the habit of just hitting Enter as soon as a
message box pops up without reading it first, and then going "oh, shoot!" -
too late). i sometimes change the default focus on multi-button messages
boxes, as

MsgBox("Changes have been made to the Applicant ID" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo + vbDefaultButton2, "Changes Made...")

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top