VB exit Window

L

Luisa

Hello,
When YES is used in the below window, the form saves the information and
closes itself.
When NO is used, an error message tells me that some is wrong with
'DoCmd.RunCommand acCmdUndo' but I don't see the problem.
Can someone help?
TIA
Luisa

---------------------------------------------

Private Sub Command21_Exit(Cancel As Integer)
Dim strMsg As String
strMsg = "Ha hecho cambios."
strMsg = strMsg & " ¿Desea guardar los campos?"
'strMsg = strMsg & "@Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "¿Guardar cambios?") = vbYes
Then
'do nothing
Else
DoCmd.RunCommand acCmdUndo

End If
End Sub
 
P

Paul Overway

The problem is that there may not be anything to undo. You need to trap the
error or ignore it using On Error Resume Next
 
B

Bruce M. Thompson

Luisa:

The code you have provided does not close the form and you have used an unusual
event to trigger your code in the first place. Normally, one would use the "On
Click" event procedure to run this type of code from a command button (your code
only runs when you move the focus away from the command button).

I would suggest you move your code to the command button's "On Click" event
procedure and modify it to read something like this:

'************EXAMPLE START
Private Sub Command21_Click()
Dim strMsg As String
strMsg = "Ha hecho cambios."
strMsg = strMsg & " ¿Desea guardar los campos?"
'strMsg = strMsg & "@Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "¿Guardar cambios?") = vbYes Then
'Ensure that the record gets saved
If Me.Dirty Then
Me.Dirty = False
End If
'If you want to close the form, you must include a line of code to do
so
DoCmd.Close acForm, Me.Name
Else 'Undo changes to this record
'If changes have been made, undo them
If Me.Dirty Then
Me.Undo
End If

'If you want to close the form, you must include a line of code to do
so
DoCmd.Close acForm, Me.Name
End If
End Sub
'************EXAMPLE END

Remember that if the user closes the form using any method other than your
command button, that code will not run.
 

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