Save records on Main form with subform in one Button

mba_110

Registered User.
Local time
Today, 11:14
Joined
Jan 20, 2015
Messages
280
Hi everybody

I have main form and a sub form on it and sub form data source is query, I am trying to figure out if i can save my main form with subform in one save command button, also it should not irritate me by asking to save each time i move my cursor to subform.

I have following code ready for main form and i want to tailor it to include my above requirement.

Code:
Option Compare Database

Private Sub BtnSave_Click()
saved = True
   DoCmd.RunCommand (acCmdSaveRecord)
   Me.BtnSave.Enabled = False
   saved = False

End Sub

Private Sub CmdNew_Click()
DoCmd.OpenForm "frmAddnewEntry"
End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Response As Integer
If saved = False Then
    Response = MsgBox("Do you want to save the changes on this record?", vbYesNo, "Save Changes?")
    If Response = vbNo Then
       Me.Undo
    End If
    Me.BtnSave.Enabled = False
End If
End Sub

Private Sub Form_Dirty(Cancel As Integer)
Me.BtnSave.Enabled = True
End Sub

Private Sub txtEntryNo_AfterUpdate()
Me.Recordset.FindFirst "EntryNo = " & Nz(txtEntryNo, 0)

End Sub

I hope i make sense in above details.
 
If your main form is bound to a table or updateable query, then simply moving to the subform will automatically trigger a save which will trigger your main forms before update code.

The only way around this is to either validate the data in the main form and check if the changes are acceptable and not offer the opportunity to cancel the changes.

You cannot not save the data as it may be required for the subform to perform correctly, so this is the inbuilt behaviour.

The only other option is Unbound forms, which are a real pain to program and maintain, and normally not required.
 

Users who are viewing this thread

Back
Top Bottom