clear&exit button

thewiseguy

Registered User.
Local time
Today, 19:47
Joined
Apr 14, 2004
Messages
56
hullo there -

i'm using the following code for a 'clear & exit' button


Code:
If MsgBox("Are you sure you wish to clear the current record?", 
vbQuestion + vbYesNo, "Clear Record Verifification") = vbYes Then
Me.Undo
DoCmd.Close
DoCmd.OpenForm "frm: MAIN MENU"
GoTo Exit_IVClearExitButton_Click
this works fine unless the user has reached one of the tabforms (further down on the mainform) - then i'm guessing, the record has updated and Me.Undo has no effect. I've tried using:


Code:
DoCmd.RunCommand acCmdDeleteRecord
...but I keep getting a "No Record Available" error messege popping up.

Any clues as to where I'm doing wrong??
Cheers.
 
Rich said:
Use the Before Update event of the form
how would i do use the before update event when i'm using the onclick event for this 'clear & exit' button?
 
You can't, so hide the tabs until they've decided whether or not they want to save the record
 
Rich said:
You can't, so hide the tabs until they've decided whether or not they want to save the record
sorry for being fussy, but i'd rather it give the user the option at any point of clearing and exiting (rather than have a 'check save' decision point before entering the first tabform.)
can i ask what i was doing wrong with the acCmdDeleteRecord?
 
As Rich so wisely stated, you have to check if the record is about to be saved in the forms BeforeUpdate event. That will allow you to prompt the user if they want to save the record changes [before they are saved] or undo their current changes.

This will do that...
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err

    If MsgBox("The current record has been altered!  Do you wish to save your changes?  Click 'Yes' to Save  or  'No' to Discard changes.", vbQuestion + vbYesNo, "Save Current Record Change?") = vbYes Then 'do nothing
        'user clicked Yes, save the changes
    Else
        DoCmd.RunCommand acCmdUndo
    End If
    
Form_BeforeUpdate_Exit:
    Exit Sub

Form_BeforeUpdate_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume Form_BeforeUpdate_Exit
    
End Sub

If the record is not dirty then there is nothing to undo. This will check if dirty and undo the changes if true...
Code:
Private Sub bUndo_Click()
On Error GoTo Err_bUndo_Click
        
    'Resets the record if it has been modified by the user.
    If Me.Dirty Then
        Beep
        DoCmd.RunCommand acCmdUndo
        Form_Current
    Else
        Beep
        MsgBox "There were no modifications made to the current record.", vbInformation, "Invalid Undo"
    End If
    
Exit_bUndo_Click:
    Exit Sub
    
Err_bUndo_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_bUndo_Click
    
End Sub
 

Users who are viewing this thread

Back
Top Bottom