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 "/" 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) <> "/" or (cArray(5) <> "/") Then
msgbox "Please use proper date format: dd/mm/yyyy"
else if (x <> 9) then
msgbox "Please use four-digit year: dd/mm/yyyy"
end if
Hope this works for you
carray = split(txtdate.text)
if ((cArray(2) <> "/" or (cArray(5) <> "/") then
msgbox "please use the correct format: mm/dd/yyyy"
else if(ubound(cArray) 9) then
msgbox "please use 4-digit years: mm/dd/yyyy"
end if 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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.