Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012
    Posts
    6

    Unanswered: Exit sub on close event

    CAN SOMEONE TELL ME WHY THE CODE BELOW WILL NOT ALLOW THE FORM TO REMAIN OPEN AFTER CLICKING OK ON THE MESSAGE DIALOG BOX? AFTER I CLICK THE (X) CLOSE BUTTON THE MESSAGE POPS UP AND AFTER CLICKING OK BOTH THE MESSAGE AND THE FORM CLOSE

    CLOSE EVENT
    If IsNull(Me.TXT_BOX_DATE) Then
    MsgBox ("MESSAGE."), vbCritical
    Me.TXT_BOX_DATE.SetFocus
    Exit Sub
    End If

    THANK YOU

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Within the Form_Close event handler, the closing process has already begun. Leaving the Form_Close event handler (as you do with an Exit Sub instruction) does not interrupt this process. The only way to prevent a form from closing at this stage is within the Form_Unload event handler:
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        Cancel = True
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    And in point of fact, this type of Validation Code actually belongs in the Form_BeforeUpdate event, using Cancel = True, as Sinndho suggested:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If IsNull(Me.TXT_BOX_DATE) Then
      Cancel = True
      MsgBox ("MESSAGE."), vbCritical
      Me.TXT_BOX_DATE.SetFocus
     End If
    End Sub


    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •