Save Button - Saves both Main Form and Subform

RogueJD

Access Beginner
Local time
Tomorrow, 01:47
Joined
Jan 13, 2010
Messages
30
Hey all.

I am looking for a way to save both the form and subform with the click of a single command button located on the main form.

I'm aware that the fields auto update when you go from parent to child, but that's not what I want.

For this form, it's important that the forms do not auto update when switching from parent to child, or when switching records.

One should be able to switch from parent to child form without being prompted to save (But that's not that important.) Ideally, one should only be prompted to save when they click the button.

Right now, I have the following code for the main form. It seems to do what I want, but it only applies to the main form. I'm aware I can use the same code for the subform, but I don't want to have to click on save twice.

Code:
Private Sub Form_BeforeUpdate (Cancel as Integer)
Dim strMsg As String
Dim iResponse as Integer
 
' This is the message that will display afor the command prompt:
strMsg = "Do you wish to save your chagnes?" &Chr (10)
strMsg = "Click Yes to Save, or No to discard changes."
 
'Displays the message box:
iResponse = MsgBox (strMsg, vbQuestion + vbYesNo, "Save Record?"
' Check response:
If iResponse = vbNo Then
' Undo the change
DoCmd.RunCommand acCmdUndo
 
' Cancel the Update:
Cancel = True
End If
End Sub

I looked up some old threads, but I didn't find anything that worked for me.

Any suggestions?

-J. "Jon" Rogue
 
Oh - the other problem is that the code requires me to press save if I edit the data - even if I press the Save Command button.

This is the code for my "Save" Button.
Code:
Private Sub Save_Click ()
Dim LResponse As Integer
Dim LMsg As String
LMsg = "Do you wish to save changes?"
LResponse = MsgBox (LMsg, vbYesNo, "Save Changes?")
If LResponse =vbNo Then
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
End If
 
End Sub
Again - I'd like everything to be done by the save button. I don't want the forms to be updated until the save button is pressed, but I don't want to have to click save 2-3 times.
 
Hi Rogue, try this

Code:
 Function SaveRecord() As Boolean
On Error GoTo SaveRecord_Error
    Application.RunCommand acCmdSaveRecord
    SaveRecord = True
SaveRecord_Exit:
    Exit Function
SaveRecord_Error:
    SaveRecord = False
End Function
it should take away the need to click save.

EDIT:

You can add a msgbox if you like to ask the question on saving. Then the true value of that would call the saverecord Function.
HTH

Just use the command "SaveRecord" in the VBA code for where you need it.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom