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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make sure no extra code runs when closing form 1

Status
Not open for further replies.

cac2

Programmer
May 7, 2002
7
US
I have the following code on a subform, have tried putting it to different events, but can only get it to run the way I really want (not allowing you out of the begdate field until it's okay) unless I have it on the exit event. However, when I close the main form this code runs again (no matter where on the form or its many subforms I am on immediately before clicking the close button), and since the main form is closed it cannot find the dob it needs in the eighth line, so every time I close the main form I get an error. Is there code I can put in my closing routine to get this out of the way before I actually close the form?

Private Sub txtBegDate_Exit(Cancel As Integer)
Dim strmesg As String

If DateDiff(&quot;d&quot;, [BEG_DATE], Now()) < 0 Then
strmesg = &quot;Start Date must be previous to today's date.&quot;
Cancel = True
TxtBegDate.setfocus
msgbox strmesg, vbOKOnly, &quot;Invalid Start Date&quot;
Exit Sub
End If
If Me.TxtBegDate < [Forms]![Frm_MainStu]![txtDOB] Then
If Me.TxtBegDate <> #1/1/45# Then
strmesg = &quot;Start date must be later than student's date of birth. Please check.&quot;
Cancel = True
TxtBegDate.setfocus
msgbox strmesg, vbOKOnly, &quot;Invalid Start Date&quot;
End If
End If
End Sub
 
There is a quick and dirty way round this .. ..


Private Sub txtBegDate_Exit(Cancel As Integer)
Dim strmesg As String
On Error Goto Err_txtBegDate_Exit

If DateDiff(&quot;d&quot;, [BEG_DATE], Now()) < 0 Then
strmesg = &quot;Start Date must be previous to today's date.&quot;
Cancel = True
TxtBegDate.setfocus
msgbox strmesg, vbOKOnly, &quot;Invalid Start Date&quot;
Exit Sub
End If
If Me.TxtBegDate < [Forms]![Frm_MainStu]![txtDOB] Then
If Me.TxtBegDate <> #1/1/45# Then
strmesg = &quot;Start date must be later than student's date of birth. Please check.&quot;
Cancel = True
TxtBegDate.setfocus
msgbox strmesg, vbOKOnly, &quot;Invalid Start Date&quot;
End If
End If

Err_txtBegDate_Exit:

End Sub



This will cause the Sub to exit as soon as it comes across an error ( any error ) and so 'softly and silently fade away'.


'ope-that-'elps.

G LS
 
Just to protect yourself, I would enhance the above suggestion as below. That way you will know about unexpected errors, should one occur. Also, as an aside, if you set Cancel = True, there is no need to setfocus because it will automatically be returned to the control in question.

txtBegDate_Exit_End:
Exit Sub

Err_txtBegDate_Exit:
Select Case Err.Number
Case your specific number
GoTo txtBegDate_Exit_End
Case Else
Msgbox Err.Description & vbCrLf & Err.Number
End Select

End Sub

End Sub
 
A more good method would be have a fbFormUnload and set
it in Form_Unload event

So check that as the first line and if true exit
Set the bool as false in Form_Open
and true in Form_Unload
 
Am I the only one who thinks this behavior is totally wrong and unacceptable? I think he wants this code to run for a specific event, not necessarily while the form is in the process of closing. Might it not be possible he has a corrupted form and should go through the process of at least trying to treat it as such before looking at code that attempts to resolve a problem which should not be occurring at all.

Just my $0.02 worth.
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top