Link to home
Start Free TrialLog in
Avatar of Matthew Tandy
Matthew TandyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Getting dates from a user

whats the best way to get a date from a user, and to ensure that the date is not like 99/99/9999

Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

1) add the DatePicker Control to your form

2) use ComboBoxes for Month, Day, and Year

3) use a Masked Edit Control

any on these will simplify the validation process.

AW
Avatar of JohnMcCann
JohnMcCann

Use the DTPicker control found in Microsoft Common Controls 2
If you have vb 5, then you may not have the DTPicker control, so you can use a plain text field and validation is something like :

theDate = Text1.Text
If IsDate(theDate) Then
    theDate = Format(theDate, "Short Date")
    Text1.Text = theDate
Else
    MsgBox "Invalid Date"
End If

Avatar of Mayank S
1. If you want to take it as input, then you can ask the user to input it in mm/dd/yyyy format:

Dim str As String
Dim tempDate As Date

str = InputBox ("Enter date in mm/dd/yyyy format: ", "DATE ENTRY")
If Not IsDate (str) Then
  MsgBox "Invalid date. "
Else
  tempDate = CDate (str)
End If

2. Otherwise, you can use combo-boxes. Let's say that you have 3 combo-boxes named dt, mn, and yr. dt contains 1, 2, 3, ...., 31 (in string form); mn contains January, February, ...., December, and yr contains 1900, 1901, .... 2003 (in string form - whatever range of years you need).

Dim str As String
Dim tempDate As Date

str = mn & " " & dt & ", " & yr
If Not IsDate(str) Then
  MsgBox "Invalid date. "
Else
  tempDate = CDate (str)
End If


Hope that helps!

Mayank.
if you use the keypress event you can trap exactly what the operator is typing.  If you don't want the "/" character you can make keyascii=0 you can block all characters except the ones you want. Example:

Private Sub Text1_KeyPress(KeyAscii As Integer)
' only allow 0 to 9 or dash
If InStr("0123456789-", Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
End If
End Sub

Hope this helps;~)


inthedark,

that DOES NOT prevent the entry of INVALID dates, such as 99/99/9999 or -9-9/9-9/9-9-9-9
or even --9/-9--/-9  and so on, ad nauseum.

Yes Arthor you already said how to validate result.  I just added how to check as data is being entered, which was a concept new to this thread.  I could also add loads of other suggestions too.
Avatar of Matthew Tandy

ASKER

is the no way to have a text box that already conatins

__/__/____

and it only allows you to type a date in?

Like the input mask in access
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial