-
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?
-
Oatorboy,
Post your code so that others can take a look at it. It would be easier to help you then.
-
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
-
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
-
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.
-
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
-
Forum Rules
|
|