How to Save a Record and Close the Form

reddevil1

Registered User.
Local time
Today, 06:36
Joined
Nov 12, 2012
Messages
92
Code:
Private Sub cmdExitForm_Click()

On Error GoTo Err_cmdExitForm_Click
If MsgBox("Do you want to Save the record?", vbYesNo + vbInformation, "Please confirm:") = vbYes Then
   If Me.Parent.Dirty = True Then Me.Parent.Dirty = False
    DoCmd.Close

Exit_cmdExitForm_Click:
    Exit Sub

Err_cmdExitForm_Click:
    MsgBox Err.Description
    Resume Exit_cmdExitForm_Click
    
End Sub

any clues to where i am going wrong?

I have been trying to create correct code for a command button which will ask if the user if he wants to save a record before exit, if the record has not been saved.

hehehe - i have tried so many ways but not been successful. The error message is Block If without EndIf. But i hav erecieved so many other error messages when i try to change the code
smile.gif
 
Try this:
Code:
Private Sub cmdExitForm_Click()
 
On Error GoTo Err_cmdExitForm_Click
 
   If Me.Parent.Dirty = True Then 
     If MsgBox("Do you want to Save the record?", vbYesNo + vbInformation, "Please confirm:") = vbYes Then
       Me.Parent.Dirty = False
     Else
       Me.Parent.Undo
     EndIf
  EndIf
  DoCmd.Close
 
Exit_cmdExitForm_Click:
    Exit Sub
 
Err_cmdExitForm_Click:
    MsgBox Err.Description
    Resume Exit_cmdExitForm_Click
 
End Sub
 
hi Bob,

thanks very much for your input.

although i am not an expert!! the code certainly looks a lot neater now. i am learning where to put the EndIf statements.

however, the Form is closing before the MsgBox appears. So it is saving the data all the time?

not sure how to resolve this coz the code seems to call the MsgBox early in the code?
 
I'm only guessing, but I suspect the code is not in a sub form. Perhaps this would work:
Code:
Private Sub cmdExitForm_Click()
 
On Error GoTo Err_cmdExitForm_Click
 
   If Me.Dirty = True Then 
     If MsgBox("Do you want to Save the record?", vbYesNo + vbInformation, "Please confirm:") = vbYes Then
       Me.Dirty = False
     Else
       Me.Undo
     EndIf
  EndIf
  DoCmd.Close
 
Exit_cmdExitForm_Click:
    Exit Sub
 
Err_cmdExitForm_Click:
    MsgBox Err.Description
    Resume Exit_cmdExitForm_Click
 
End Sub
 
thanks again bob.

the menu where the command button is located is in a subform. so the parent aspect is required.

i will fiddle with it and get it to work.

cheers.
 
Post a copy of the db in A2003 mdb format if you are unable to get it working as required and I will try to look at it.
 

Users who are viewing this thread

Back
Top Bottom