Stop generating message boxes on form close

galaxy

Registered User.
Local time
Today, 06:16
Joined
Feb 26, 2013
Messages
16
I have some code (see below) that checks if the start date text box is blank or greater than the end date, and if so, generates a message box and sets the focus back to the start date textbox. There are command buttons on the form that open queries based on the date fields. This all works fine.

However, if the user changes his mind and closes the form without filling in the date field, the message box "Please enter a start date" pops up repeatedly. How do I specify that a text box is required UNLESS the user is closing the form?


Code:
Private Sub txt_startdate_LostFocus()
If ((IsNull(txt_startdate) Or (txt_startdate) = " ")) Then
    MsgBox "Please enter a start date.", vbOKOnly
        Me.txt_enddate.SetFocus
        Me.txt_startdate.SetFocus
    End If

    If Me.txt_enddate < Me.txt_startdate Then
        MsgBox "The start date you entered is in the future.", vbOKOnly
            Me.txt_enddate.SetFocus
            Me.txt_startdate.SetFocus
        End If
Exit Sub
End Sub
 
Hello Galaxy, could this piece of validation not go into the button click? You only need it to be Correct and Not Null before you open the Query right? So putting the code into the button would be more ideal !!

This will avoid the popping up of boxes, also make sure the Query is not left with empty or wrong parameters..

Code:
Private Sub qryButton_Click()
    If ((IsNull(txt_startdate) Or (txt_startdate) = " ")) Then
        MsgBox "Please enter a start date.", vbOKOnly
        Me.txt_startdate.SetFocus
        Exit Sub
    End If

    If Me.txt_enddate < Me.txt_startdate Then
        MsgBox "The start date you entered is in the future.", vbOKOnly
        Me.txt_startdate.SetFocus
        Exit Sub
    End If
    
[COLOR=Green]    'Your code to open Query[/COLOR]
End Sub
 
I thought of this, but there are 10 possible queries the user can run from this form, and repeating the same code for all ten buttons seemed unnecessary. I thought there must be an easier way to do it. But maybe I'm wrong and it does need to be coded for each button? Seems like a pain if I have to add more queries in the future.
 
Do all the buttons check the same condition? If so you can use a Common function to test the validity and just use a call to that function maybe?
 

Users who are viewing this thread

Back
Top Bottom