Cancel record navigation?

A

Angela

I would like to confirm a record change before the user navigates to another
record. If the employee record has been changed, I want to ask the user if
they want to save the changes before moving to another record using
yes/no/cancel where cancel leaves them on the current record. I believe the
coding belongs in the "oncurrent" event, something like this:

Dim strResp As String
If Me.Dirty Then
strResp = MsgBox("You have made changes to this employee record. Do
want to save these changes before moving to another record?", vbYesNoCancel,
"Save Changes?")
If stresp = vbNo Then
Me.Undo
Resume Next
ElseIf strResp = vbCancel Then
Cancel
Else
Resume Next
End If
End If

The only problem is that there is no Cancel in this event, so how can I
cancel the navigation to another record?

Thanks
 
D

Dirk Goldgar

Angela said:
I would like to confirm a record change before the user navigates to
another
record. If the employee record has been changed, I want to ask the user
if
they want to save the changes before moving to another record using
yes/no/cancel where cancel leaves them on the current record. I believe
the
coding belongs in the "oncurrent" event, something like this:

Dim strResp As String
If Me.Dirty Then
strResp = MsgBox("You have made changes to this employee record.
Do
want to save these changes before moving to another record?",
vbYesNoCancel,
"Save Changes?")
If stresp = vbNo Then
Me.Undo
Resume Next
ElseIf strResp = vbCancel Then
Cancel
Else
Resume Next
End If
End If

The only problem is that there is no Cancel in this event, so how can I
cancel the navigation to another record?


This would normally go in the BeforeUpdate event. That event only fires
when the user has changed the record in some way (so you don't have to check
if Me.Dirty), and cancelling the event cancels the move to a new record. So
you might code it like this:

'------ start of code ------
Private Sub Form_BeforeUpdate(Cancel As Integer)

Select Case MsgBox( _
"You have made changes to this employee record. " & _
"Do you want to save these changes before moving " & _
"to another record?", _
vbYesNoCancel, _
"Save Changes?")

Case vbNo
Me.Undo

Case vbCancel
Cancel = True

Case Else
' Do nothing, let the record be saved.

End Select

End Sub
'------ end of code ------
 
A

Angela

Thank you! That is exactly what I needed.

Dirk Goldgar said:
This would normally go in the BeforeUpdate event. That event only fires
when the user has changed the record in some way (so you don't have to check
if Me.Dirty), and cancelling the event cancels the move to a new record. So
you might code it like this:

'------ start of code ------
Private Sub Form_BeforeUpdate(Cancel As Integer)

Select Case MsgBox( _
"You have made changes to this employee record. " & _
"Do you want to save these changes before moving " & _
"to another record?", _
vbYesNoCancel, _
"Save Changes?")

Case vbNo
Me.Undo

Case vbCancel
Cancel = True

Case Else
' Do nothing, let the record be saved.

End Select

End Sub
'------ end of code ------

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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