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

checking format 1

Status
Not open for further replies.

adamr1001

MIS
Jul 25, 2001
166
CA
i'd like to check a txtbox to see if it has the folloing date format:

mm/dd/yyyy

i am looking for somehting like this:

If txtDATE.text <> ##/##/#### Then
MsgBox Incorrect formula etc...
End If

what is the correct code for the ##/... part?

Thanks!
 
I would aproach this problem differently: namely at the input level. Rather than telling the user that he has input incorrect info afterwards, why not disable the user from inputting incorrect info in the first place?

This can be done in several ways:

- The easiest is using the Microsoft Masked Edit Control. this control will only accept certain values in pre-specified formats (you can also custom format your values.

- The harder, but more complete (depending on your needs), way would be to code the validation routines yourself, using the KeyPress routine to capture the input as the user presses the keys, and disable certain types of input, manipulate the input dynamically (like adding &quot;/&quot; when entering a date without the user having to type it).

For ease of use and speed, definately try the masked edit control first. Then, if that doesn't meet your requirements, start thinking of how do a validation routine.

Of course you can always still do it the way you were thinking of (with displaying error message and all):

If txtDATE.text <> ##/##/#### Then

Like this:

Dim cArray() as String
Dim i as Integer
Dim x as integer

x = len(txtDate.text)
redim cArray(0 to x)
For i = 0 to x
cArray(i) = mid(txtDate.Text,i+1,1)
Next i
if (cArray(2) <> &quot;/&quot;) or (cArray(5) <> &quot;/&quot;)) Then
msgbox &quot;Please use proper date format: dd/mm/yyyy&quot;
else if (x <> 9) then
msgbox &quot;Please use four-digit year: dd/mm/yyyy&quot;
end if

Hope this works for you :)
carray = split(txtdate.text)
if ((cArray(2) <> &quot;/&quot;) or (cArray(5) <> &quot;/&quot;)) then
msgbox &quot;please use the correct format: mm/dd/yyyy&quot;
else if(ubound(cArray) 9) then
msgbox &quot;please use 4-digit years: mm/dd/yyyy&quot;
end if Take Care,
Mike
 
oops, disregard the part using split... i forgot to delete that. Take Care,
Mike
 
Michael, I agree with your suggestion to avoid entering a wrong date into the textbox in the first place. If this approach is acceptable, I then would tend to use the MonthView or the DateTimePicker Control. _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Sure, that works even better, if all you are looking for is a date. I admit, I forgot about the monthview and datepicker controls.
However, I was thinking in a broader sense, as in telephone numbers, zip codes, etc.
In those cases you would use the Masked Edit Control.
Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top