Results 1 to 6 of 6

Thread: Terminating using VB

  1. #1
    Join Date
    Jan 2003
    Posts
    30

    Terminating using VB

    I am currently creating message boxes for the ACCESS forms using VB. The scenario is like this, the message box will prompt the user if some of the important fields are left empty when the user is exiting the form. However, after the user click ok, the form will still exit. Is there any way i can prevent the form from exiting if such cases happen?


  2. #2
    Join Date
    Apr 2003
    Location
    NJ
    Posts
    1
    Oatorboy,

    Post your code so that others can take a look at it. It would be easier to help you then.

  3. #3
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277
    Is your form bound to a recordset ? If so, you can place the code in the beforeinsert event and use a Docmd.cancelevent to stop the form closing if some fields are blank or incorrectly filled in............

    If IsNull(Me.Name) Or Me.Name = "" Then
    MsgBox "You must choose a Name", vbOKOnly + vbExclamation, "Missing Name"
    Me.Name.SetFocus
    DoCmd.CancelEvent
    Exit Sub
    End If

  4. #4
    Join Date
    Jan 2003
    Posts
    30
    Originally posted by monkeyman
    Oatorboy,

    Post your code so that others can take a look at it. It would be easier to help you then.
    Heres my code:

    Private Sub Form_Close()
    If IsNull(BY_WHOM) Then
    yourmsg = MsgBox("BY_WHOM cannot be empty!", 0 + vbExclamation, "Microsoft Access")
    If yourmsg = 1 Then
    Exit Sub
    End If
    End If
    End Sub

  5. #5
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277
    Private Sub Form_BeforeUpdate(Cancel As Integer)

    If IsNull(Me.BY_WHOM) or Me.BY_WHOM="" Then
    MsgBox "BY_WHOM cannot be empty!", vbOkOnly + vbExclamation, "Empty Field"
    Me.BY_WHOM.SetFocus
    docmd.Cancelevent
    Exit Sub
    End If
    End Sub
    Last edited by KnooKie; 04-09-2003 at 07:43 AM.

  6. #6
    Join Date
    Mar 2003
    Posts
    37

    Lightbulb

    Here is code I have used with success. Modify it to suit your needs (i.e., test for a null wherever). This is used as an event procedure when the form unloads. If you use an event procedure when the form closes, the form has actually already closed.

    Code:
    Private Sub Form_Unload(Cancel As Integer)
    'When user clicks close, this confirmation box pops up; if user selects
    'no, then Access returns user to the form
    If MsgBox("Are you sure you want to exit?", vbYesNo, "Do you want to close?") = vbNo Then
        Cancel = True
        End If
    End Sub

Posting Permissions

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