Textbox Validation - On Exit?

Celestz

Registered User.
Local time
Tomorrow, 05:07
Joined
Feb 8, 2010
Messages
12
Hi!

I tried the search function of this forum to look for a solution for my problem but I can't seem to find it so I posted it instead, maybe I missed a few threads, not sure coz i'm quite dizzy right now.

I have a form with around 45 textboxes, but i'm currently stuck up with a simple problem if you look at it but i've been juggling 3 hours trying to accomplish it.

Here's the situation:

#1. I have a Textbox Named Date1(In the form, I have Date1-Date15)
#2. The Textbox Format is in Short Date since I need to enter valid dates
#3. I created a function that compared the entered date against 2 dates that act as the upper and lower limit of my valid date range.

Code:
Private Function CheckEntryDate(DateFrom As Date, DateTo As Date, DateEntered As Date)
    If DateEntered >= DateFrom And DateEntered <= DateTo Then
        'Valid Date!
    Else
        MsgBox "This field is required!"
        CheckEntryDate = True
    End If
End Function


#4. I'm also trying to catch the event that if the user enters the textbox and tries to exit it, a Prompt will tell the user that it is mandatory.

Code:
Private Sub Date1_Exit(Cancel As Integer)
    If IsDate(Date1.Text) Then
        Cancel = CheckEntryDate(Me!DateFrom, Me!DateTo, Me!Date1)
    ElseIf Me!Date1 = "" Then
        MsgBox "This Field is Required!"
        Cancel = True
    End If
End Sub

Can anyone help me? Whenever I try and my focus is in Date1, when I exit it, it doesn't fire the exit codes
 
Try moving the code to the OnLostFocus event. also have you tried stepping through the code? Also you do not need the .Text in the qualifier.

Another thing is that you have not checked for a null value in the date1 field. Remember 0 is a date (31/12/1899).

David
 

Users who are viewing this thread

Back
Top Bottom