Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

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

0
Matthew Tandy
Asked:
Matthew Tandy
1 Solution
 
Arthur_WoodCommented:
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
0
 
JohnMcCannCommented:
Use the DTPicker control found in Microsoft Common Controls 2
0
 
BandyCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
inthedarkCommented:
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;~)


0
 
Arthur_WoodCommented:
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.

0
 
inthedarkCommented:
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.
0
 
Matthew TandyAuthor Commented:
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
0
 
Arthur_WoodCommented:
use the Masked Edit Control in VB (Projects/Components/Microsoft Masked Edit Control).  That is what the control is designed to do, and why I suggested it as a possiblity, in my original post, above.

AW
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now