?
Solved

Getting dates from a user

Posted on 2003-03-18
9
Medium Priority
?
144 Views
Last Modified: 2010-05-01
whats the best way to get a date from a user, and to ensure that the date is not like 99/99/9999

0
Comment
Question by:Matthew Tandy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8163088
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
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8163092
Use the DTPicker control found in Microsoft Common Controls 2
0
 
LVL 1

Expert Comment

by:Bandy
ID: 8163331
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8164649
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
 
LVL 17

Expert Comment

by:inthedark
ID: 8166159
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8170448
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
 
LVL 17

Expert Comment

by:inthedark
ID: 8172384
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
 
LVL 1

Author Comment

by:Matthew Tandy
ID: 8177668
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 1200 total points
ID: 8177903
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question