How to stop records from saving when moving from main form to subform?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kaloyan Krumov
    New Member
    • Oct 2010
    • 16

    How to stop records from saving when moving from main form to subform?

    I have the following set up:
    Tables
    Supplier
    SupplierContact s
    Forms
    SupplierRegform
    SupplierContact s - subform

    On the main form I’m using tab control with two tabs. The first tab contains controls for entering supplier registration information and the second tab contains the subform for entering contacts information for the supplier. On form load i hide the second tab and i set .visible property only if all fields in main form are filled. Once the user presses the button the and all fields are filled in the first tab is hidden and the second one is visualised, but in this moment if i return to the first tab (on second tab i have another button "back to main") and try to exit the form (button here for exit) the record is not deleted. Here is the code for close button:
    Code:
    If Me.Dirty Then
        If msgbox("Do you realy want to exit?", vbYesNo, "Exit") = vbYes Then
            Me.Undo
            DoCmd.Close
        Else
            SavRec.SetFocus
        End If
    Else
        If msgbox("Are you sure you want to exit without saving?", vbYesNo, "Exit") = vbYes Then
            DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
            DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
            DoCmd.Close
        Else
            Me.SavRec.SetFocus
        End If
    End If
    I tried me.Undo but still nothing.
    Can anyone help me i need to stop the records in main form from saving when i move form tab to tab?
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    I'm not sure, but maybe you could put some code in the form's BeforeUpdate event. The code would check the conditions and do a docmd.canceleve nt if the record is not to be written.

    Jim

    Comment

    • Kaloyan Krumov
      New Member
      • Oct 2010
      • 16

      #3
      Tx Jim for your reply.
      This can't work as soon as the subform loses focus (me clicking back button) the records in the subform are saved. I want to stop this from happening. The changes should be saved only when i click on save button or undo all changed records both in main and subform if i press the close button.

      Comment

      • jimatqsi
        Moderator Top Contributor
        • Oct 2006
        • 1293

        #4
        So the code belongs in the subform's BeforeUpdate event, not the main form's. Make that event smart enough to know whether it should allow the save to happen or not.

        I'd add an invisible checkbox to the subform, default value of false. The subform's BeforeUpdate event could look at the value of that checkbox, if it is false it does a DoCmd.CancelEve nt. If it is true, it does not.

        The Save button on the Main form can set the value of the checkbox to True, which should force the Update to happen.

        Jim

        Jim

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32668

          #5
          I doubt you can switch with active changes in the main form. My guess (I have nothing to test with) is that the changes would need either to be reset or saved before you can switch.

          Comment

          • Kaloyan Krumov
            New Member
            • Oct 2010
            • 16

            #6
            I have tried something different.
            In main form i have this code:
            Code:
            Dim bolSubformDirty As Boolean
            
            Public Function SetMyDirty()
            bolSubformDirty = True
            End Function
            
            Private Sub ExitFRM_Click()
            
            If bolSubformDirty = True Or Me.Dirty And msgbox("Do you want to save the changed records bfere exeting?", vbYesNo, "Exit") = vbYes Then
                    DoCmd.RunMacro "[SaveRec]"
                    DoCmd.Close
                Else
                    Me.Undo
                    Me.SupplierContacts_Subform.Form.Undo
                    DoCmd.Close
                End If
            End Sub
            and in the sub form this code:

            Code:
            Private Sub Form_AfterUpdate()
            Me.Parent.SetMyDirty
            End Sub
            this works as to the point when i have to undo the changes if the user doesn’t want to save them on exit.

            As you know always on the next day you get different ideas.
            I think it will be best if the subform shows as popup window and its modal property is set to yes. This way i can control the saving of records in the two forms.
            What do you think of this?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32668

              #7
              I don't feel I have a clear enough understanding of exactly what you want out of this, so I am not in a position to comment. I wouldn't recommend use of macros myself, but if this does what you need then it only matters how happy you are with it.

              Comment

              Working...