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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to code an onClose when closing a table?

Status
Not open for further replies.

trifest

Programmer
Sep 5, 2002
52
US
I do an openTable so that the user can edit straight into the table. But how can i set it that when they go to close the table or Access to prompt them to save and then also run an append query after they click yes to saving. Thanks for the help guys. I'm new at VBA so i'm learning while i go.
 
Place the Code in the On Close event.

This way If the User close the table via the X (Close Window) or by a Command button, this code will run prior to closeing the form.

Sample Code:

Private Sub Form_Close()
Dim strMessage as String

strMessage = "Do you wish to save the Record?

If MsgBox(strMessage, vbYesNo + vbExclamation + vbDefaultButton2, "Save Record ") = vbYes Then

‘Do all my saveing SQL update, etc… here
Forms!MyOtherOpenForm.FieldName.Requery
DoCmd.RunSQL ("UPDATE YourTable SET YourField = " & Me.NewValue & ";")
Etc...
Else
‘Do something else
End If

End Sub


Pierre
 
So i place this code in the onClose event for the form or for the table. I have a form where when they click a command button it opens a table so they can edit information. So where would i put this onClose event. I guess I'm just kind of confused. So will this take care of the situation if they were to close the table or if they chose to close Access? I'm just trying to figure this out. Sorry for the idiotic questions.
 
The On Close Event is only available in Forms and Reports.

If you want to open a table, you should create a Form then Select Default View = Datasheet. (we'll Call it frmTable)

This frmTable Form will look exactly like a regular table/query, but now you have the option to enter some event Codes.

Since this frmTable is where all the Changes are happening, place your code in the On Close event of the frmTable Form.

If the frmTable is open and the User Closes Access, the On Close event for frmTable will run prior to Access Closing. But only IF frmTable form is open.


Pierre
 
I suppose my next question is how do i populate that table in a form view. do i put a query in the recordsource?
 
also when i edit the table how do i then copy into another table i created for historical information. Thanks for all the help. this has definitely been helpful
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top