Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for Form's UnLoad Event - Help

Status
Not open for further replies.

ChemistZ

Programmer
Oct 30, 2001
70
US
Could someone please look at the code below and tell me why if the County No is blank it gives me the proper message box but then the form closes anyway? I thought Cancel=True was supposed to stop that.

Dim DataErr As Integer
On Error GoTo err_try

If Me.Dirty And IsNull(Me!COUNTY_NO) Then
Cancel = True
MsgBox "You can not leave COUNTY NO blank. It is a required field. Please enter the correct COUNTY NO.", , "Service of Process Error"
Me!COUNTY_NO.SetFocus
ElseIf Me.Dirty And IsNull(Me!COURT_TYPE_CODE) Then
MsgBox "You can not leave COURT TYPE CODE blank. It is a required field. Please enter the correct COURT TYPE CODE.", , "Service of Process Error"
Me!COURT_TYPE_CODE.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!COURT_NMBR) Then
MsgBox "You can not leave COURT NUMBER blank. It is a required field. Please enter the correct COURT NUMBER.", , "Service of Process Error"
Me!COURT_NMBR.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!CASHIER_NO) Then
MsgBox "You can not leave CASHIER NUMBER blank. It is a required field. Please enter the correct CASHIER NUMBER.", , "Service of Process Error"
Me!CASHIER_NO.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!DOCKET_NO) Then
MsgBox "You can not leave DOCKET NUMBER blank. It is a required field. Please enter the correct DOCKET NUMBER.", , "Service of Process Error"
Me!DOCKET_NO.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!EXCEPTION_CODE) Then
MsgBox "You can not leave EXCEPTION CODE blank. It is a required field. Please enter the correct EXCEPTION CODE.", , "Service of Process Error"
Me!EXCEPTION_CODE.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!TRANS_DATE) Then
MsgBox "You can not leave TRANSACTION DATE blank. It is a required field. Please enter the correct TRANSACTION DATE.", , "Service of Process Error"
Me!TRANS_DATE.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!BATCH_DATE) Then
MsgBox "You can not leave BATCH DATE blank. It is a required field. Please enter the correct BATCH DATE.", , "Service of Process Error"
Me!BATCH_DATE.SetFocus
Cancel = True
ElseIf Me.Dirty And IsNull(Me!MASTER_CODE) Then
MsgBox "You can not leave MASTER CODE blank. It is a required field. Please enter the correct MASTER CODE.", , "Service of Process Error"
Me!MASTER_CODE.SetFocus
Cancel = True
Else

Exit Sub
End If
exit_unload:
Exit Sub
err_try:
If DataErr = 2279 Then
MsgBox "The EXCEPTION CODE field and the TYPE CLAIM field both require the entry of two letters. Please enter two letters only.", , "Service of Process Error"
Cancel = True
ElseIf Err.Number = 3022 Then
MsgBox "The EXCEPTION CODE, CASHIER NUMBER and DOCKET NUMBER together make up the key to this form. You have entered a combination of those three that already exists in the EXCEPTIONS table. Please try again.", , "Service of Process Error"
Me!EXCEPTION_CODE.SetFocus
Cancel = True
ElseIf Err.Number = 3201 Then
MsgBox "The combination of COUNTY NO, COURT TYPE CODE and COURT NUMBER make up a unique field that identifies a specific court. The COUNTY NO you entered into this combination does not exist. Please re-enter the COUNTY NO.", , "Service of Process Error"
Me!COUNTY_NO.SetFocus
Cancel = True
Else
Resume exit_unload
End If
End Sub
 
Hi!

I think it is because you left out the Cancel = True in the first If statement.

hth Jeff Bridgham
bridgham@purdue.edu
 
No, the Cancel=true is there just in a different order than all the others. I got a reply back saying I should use

If Not Me.Newrecord and IsNull(COUNTY_NO) then
cancel=true instead of the Me.dirty code however this is the problem with that
When a user opens the forms and does nothing at all and then hits the close button the form closes which is exactly right. However, if they start typing and then decide to hit the close button, the error message comes up telling them they can not leave certain things blank but the form then closes anyway. Why???
 
Hi again!

Sorry, I missed it. I think that the problem is occurring because the update(before and after) events have already happened. I think you need this code in the Before_Update event procedure.

hth
Jeff Bridgham
bridgham@purdue.edu
 
This code is in the Before Update procedure and I just did some testing to see which is firing and you are right. It is the Before Update procedure that is producing the message. The problem still remains - how do I prevent someone from closing the form when things are blank with the form's close button? Especially since you can not really get rid of the close button. I know you can turn it off in properties but if they maximize the form then a close button comes up anyway.
 
Hi!

One thing you can do is have a form level variable bolClose which you set to true in the form load procedure. Then in the unload procedure you put

Cancel = bolClose

When they click your exit button you set bolClose to false so the form will close.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top