Link to home
Start Free TrialLog in
Avatar of wj53
wj53

asked on

check if acUndo available

Hi,
I want to add a message box(save or not) before closing a form in ACCESS. It seems like the default is save any change when closing a form. as long as user types anything in the form I can use acUndo to discard the changes without saving it. However it user only browse it, it gives me error message says the " action Undo is not available."  How could I check if acUndo is available or not? acUndo always = 0.

here is the code,

Private Sub Form_Close()
'somecode here, if acUndo available...      
If MsgBox("Do you want to save it?", vbYesNo, "Remind") = vbNo Then
   DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, acObjectVerb, acMenuVer70
Else
   Exit Sub
End If
   
End Sub

Thanks,
wj53
Avatar of hongjun
hongjun
Flag of Singapore image

Try this

Private Sub Form_Close()
    'somecode here, if acUndo available...
    If MsgBox("Do you want to save it?", vbYesNo, "Remind") = vbNo Then
        If acUndo = 0 Then
          Exit Sub
     Else
             DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, acObjectVerb, acMenuVer70
     End If
    Else
        Exit Sub
    End If
End Sub


hongjun
Or this

Private Sub Form_Close()
On Error Resume Next
    'somecode here, if acUndo available...
    If MsgBox("Do you want to save it?", vbYesNo, "Remind") = vbNo Then
        DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, acObjectVerb, acMenuVer70
    Else
        Exit Sub
    End If
End Sub


hongjun
Ignore the first post by me. It is utter rubbish. Use the second one. It will work.

hongjun
Avatar of aikimark
You can only UNDO a user (or program code intitiated) change, so you will need to check if the form's Dirty property.

You can also check the underlying recordset's EditMode property.  Possible values that would allow UNDO are:
dbEditInProgress and dbEditAdd
Avatar of wj53
wj53

ASKER

to hongjun, I tried your method, however, if I open the form and then close it, it still ask me if i want to save it or not.(Eventhough I didn't change anything)

To aikimark, what's the syntax to check the property? I am not quiet familar with VB code.

Thanks for your response.

wj53
If me.dirty then

End If

or

Select Case rs.EditMode
  Case dbEditInProgress , dbEditAdd

End Select
Avatar of wj53

ASKER

This is what I copied for Dirty event from Help file.

"Note:Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property."

This is probably why Me.Dirty always eq False no matter I changed something or not.

I think ss long as I am able to test the value for the UNDO in the toolbar, I am all set. but how...?
You should place the code in the QueryUnload event.
Hi wj53@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

wj53@devx, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial