|
-
Aug 25th, 2001, 09:18 PM
#1
Thread Starter
Lively Member
textbox validation with FORMAT
Hi,
I am just learning VB. I am trying to get this code to work:
Private Sub txtDateOff_LostFocus()
If txtDateOff.Text = Format(txtDateOff, "dd/mm/yy") Then
Exit Sub
Else
txtDateOff.Text = ""
MsgBox "Please enter Date in dd/mm/yy format. For example, 08/25/02."
End If
End Sub
As you can probably see I am trying to have the user enter a date in a textbox and then only allow it to "stick" (and be submitted to the database) if they get the dd/mm/yy format right. Only problem is I can't get it right: When I type in, say, 08/25/01 or anything else, I get the "Pleas enter date" msgbox. I might have the FORMAT syntax wrong. Thanks a bunch for your help.
Nick
-
Aug 25th, 2001, 09:38 PM
#2
Registered User
Use the masked edit control, which are great for dates.
Alternatively, try something like (note use of validate event:
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
If IsDate(Text1) Then
Text1.BackColor = &H80000005
Text1 = FormatDateTime(Text1, vbShortDate)
Else
Cancel = True
Text1.BackColor = vbRed 'or Beachbum would recommend a msgbox right here
End If
End Sub
-
Aug 25th, 2001, 10:29 PM
#3
I think you switched the months and days in this line:
If txtDateOff.Text = Format(txtDateOff, "dd/mm/yy") Then
The code is looking for a "dd/mm/yy" date. You're giving it a "mm/dd/yy" date (08/25/01).
-
Aug 25th, 2001, 10:43 PM
#4
-
Aug 25th, 2001, 11:41 PM
#5
Thread Starter
Lively Member
thanks
Thanks all,
Tygur, you were exactly right. Pretty observant! The code works now.
Beachbum, I know what you're saying. I don't like the masked edit boxes either. Any advice on how I might do this simply with isdate ().
Nick
-
Aug 26th, 2001, 08:32 PM
#6
Registered User
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
|
Click Here to Expand Forum to Full Width
|