Form closes before errors or notifications can be posted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Belimisimus
    New Member
    • Mar 2010
    • 18

    Form closes before errors or notifications can be posted

    On close command I have code which ask me if I want to save changes before closing (yes, no, cancel).

    Code:
    Private Sub Close_Click()
        bSaving = True
        If Me.Dirty Then
            answer = MsgBox("Do you want to save changes before closing?", vbYesNoCancel + vbQuestion, "Confirm Save")
            If answer = vbNo Then
                Me.Undo
                DoCmd.Close acForm, Me.Name
            
            ElseIf answer = vbYes Then
                On Error Resume Next
                DoCmd.RunCommand acCmdSaveRecord
                DoCmd.Close acForm, Me.Name
            End If
        Else
            DoCmd.Close acForm, Me.Name
        End If
        bSaving = False
    End Sub

    If I say yes then in before update there is a code which checks if all required fields are filled.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me![cbo_AssignedFrom]) Then
    MsgBox "You must enter who is Assign from before this Record can be saved"
    Cancel = True: Me![cbo_AssignedFrom].SetFocus
    In case they aren't it notify me but still form is close. How to stop closing in that case?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32653

    #2
    The Cancel parameter in a Form_BeforeUpda te() event procedure simply specifies whether the Update should proceed. It has no effect on any related closing of the form. There wouldn't necessarily even be a related close if the save were called from elsewhere for instance.

    Furthermore, there is no BeforeClose event to handle. The facility to cancel such a close is not supported. You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32653

      #3
      By the way, your Close_Click() procedure closes even when the operator chooses Cancel (vbCancel). This makes little sense. If you don't want to offer the facility to cancel then use the vbYesNo option instead.

      Comment

      • Belimisimus
        New Member
        • Mar 2010
        • 18

        #4
        Originally posted by NeoPa
        By the way, your Close_Click() procedure closes even when the operator chooses Cancel (vbCancel). This makes little sense. If you don't want to offer the facility to cancel then use the vbYesNo option instead.
        The cancel option is working. It stops closing and there is no questions from before update code!?


        You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.
        I'm newbie about vba, and most of codes I find (learn) surfing the net... could you give me an example of code for this solution?

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32653

          #5
          Originally posted by Belimisimus
          The cancel option is working. It stops closing
          You are quite right. My mistake. I misread your code.
          Originally posted by Belimisimus
          there is no questions from before update code!?
          What are you talking about? This is after a quote which doesn't mention this. I'm confused about what you're trying to say.
          Originally posted by Belimisimus
          I'm newbie about vba, and most of codes I find (learn) surfing the net... could you give me an example of code for this solution?
          I'm happy to work with you, but I won't simply produce code for you. That said, if you make a try and tell me where you get confused or stuck I can certainly help.

          Comment

          • Belimisimus
            New Member
            • Mar 2010
            • 18

            #6
            I really appreciate your help and I didn't want from you to write a code instead of me. All I didn't know what the "Check the Err object" means so I ask for example (some times better way of explanation).

            One way or another your solution was helpfull. Code below is working for me...hope that this is what you sugesst!

            Code:
            "On Error GoTo Close_Click_Err" 
            ...code...
            Close_Click_Exit:
                Exit Sub
            Close_Click_Err:
                'MsgBox Error$
                Resume Close_Click_Exit
            Thx again!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32653

              #7
              That makes better sense. I will explain the Err object further (but for a better understanding simply type in "Err" on a new line in your code and press F1 for context-sensitive help).

              Err is set after any executed piece of code that can fail. There are many possible failures, and Err will give a Number and Description for whatever caused the problem.

              The following code gives a very brief illustration :
              Code:
              On Error Resume Next
              Call DoCmd.OpenForm(Forms(-1))
              If Err.Number = 2456 Then
                  Call MsgBox(Err.Description)
              End If

              Comment

              Working...