Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Close,Save,Cancel...

Status
Not open for further replies.

uncchcs

Programmer
Jun 4, 2002
13
US
I have a form with buttons where users can save,cancel,delete,create new, or close the form. What I want to do, is if the user attempts to close the form after changing data without clicking save, prompt them on close to accept or cancel changes. The same if they attempt to close with the close button. Anyone have any suggestions?

Jason
 
Jason,

Have you tried putting code behind the OnClose event property of your form? The code could trigger a message box that could say "Would you like to save the changes?" and vbOKCancel or vbYesNo, and then if the answer if OK or Yes, run code or a macro that saves the changes?

lastout lastout (the game's not over till it's over)
 
This is the code I am using:

Private Sub FormDirty()
Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to save changes to the current record?"
Style = "vbYesNoCancel"
Title = "Save changes"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
MyString = "Yes"
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
ElseIf Response = vbNo Then
MyString = "No"
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
DoCmd.Close
Else
MyString = "Cancel"
End If
End Sub

Private Sub Form_Close()
Call FormDirty
End Sub

I keep getting a type mismatch error.
Any suggestions?
 
Jason,

I'm not sure what you're exact needs are. Generally forms will save newly entered data without anyone hitting the save button. If not, when they attempt to close the form they'll get the Access message that asks them if they want to save the changes. It sounds like you have a lot of buttons on one form, some of which may not be necessary to your task. It also sounds like you want to compel the user to click one of the buttons on the form before closing, and when they close, they must use the close button? I may not be understanding you correctly, but just in case, you can remove the (x) close button (in the upper right corner) to force the user to use one of your buttons to close the form. If the user hits btnSave, then the rest is moot. If the user clicks btnClose, then you can trigger a msgbox that asks if the user wants to save the changes before closing. Am I understanding you correctly? lastout (the game's not over till it's over)
 
OK Here is what I want the form to do:
User opens form and hits new to create new record...
If user hits save, then either (X) or btnClose, the form will close.
If user doesn't hit save, then when closing with either (X) or btnClose, prompt user with Yes,No,Cancel
Yes will save record and close the form
No cancel record without saving
Cancel will return to form without closing
Same scenario is user navigates to a record already entered.
If they change any of the date, then when closing make sure they want to accept the changes.
Also I have a delete button on the form.
If they hit the delete button, I want to prompt the user with a message "Are you sure you wish to delete the current record?" If yes, delete record, if no return to form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top