Sep 19th, 2007, 04:30 PM
Classic VB - What controls can I use to input a date/time?
There are a few different ways that you can safely get date values from the user, including special date/time controls which are provided for you.
These controls provide the extra advantages of being a much better interface for the user, and they do lots of the work for you - for example they will only allow the user to input valid dates, and not things like "13/13/2007"!
Here are some of the controls that are commonly used:
MonthView and DateTimePicker controls
These are two of the most popular date/time controls, and are included in the component "Microsoft Windows Common Controls-2 6.0". To add that to your project, go to "Project" -> "Components", scroll down the list until you find it, then tick the box next to it, and click "OK". You should now see these icons in your toolbox:
The MonthView control allows the user to select a date (not a time), and if you add to a form you will see that it looks like this:
The user can click on a date to select it, and they can use the arrows at the top to change months, as well as clicking on the month/year text to get extra navigation options.
To get or set the value of this control, use the .Value property, eg:
The DateTimePicker control allows the user to select a date and/or a time, as you specify. When you add it to your form, it will look like the first of these:
MonthView1.Value = Date 'select today's date
Dim MyDate as Date
MyDate = MonthView1.Value 'read the selected date into our Date variable
If you right-click on it an select Properties, you can set whichever display format you like (such as dtpTime, or a custom one), as I did for the second and third above.
As well as allowing text based entry (which is automatically validated), if a date is part of the format you specified then it allows the user to press the arrow on the right, and what looks suspiciously like a MonthView control appears for them to select from!
To get or set the value of the DateTimePicker, use the .Value property in the same way as the example code for the MonthView above (this applies if you are using Date only, Time only, or Date and Time).
The Calendar control (VBA only, not safe for VB!)
If you are using VBA (the VB editor inside Office applications), you will probably also have the option of the Calendar control (which you can add by clicking on the "..." icon at the bottom of the toolbox), but note that you may not be allowed to distribute it to your users (further details).
This control is not suitable for standalone versions of VB (outside of the VB editor), as apart from the fact you aren't allowed to distribute it, it has graphical issues when used from outside of Office applications.
If you for any reason your can't (or don't want to) use any date/time specific controls, a safe option is to use separate inputs for each part of the date (eg: one textbox/combobox for the month, one for the year, etc).
Here is an example of how 3 ComboBoxes could look:
This will take more work than the options above, as you will need to ensure only valid dates are entered (and not values like February 31st!).
Once you have validated the input, you can safely store the value to a Date variable/properly by using the DateSerial function, eg:
A single input
'Assumes intMonth/intDay/intYear are Integer variables which have been validated:
Dim MyDate as Date
MyDate = DateSerial(intYear, intMonth, intDay)
If you are forced to use input from a single String based source (like a TextBox or InputBox), you will be prone to conversion errors.
When you convert a String value to a Date value, it is possible that the value will be misinterpreted, and so your program will not get the same date as was intended (if the user meant August the 9th, your program may think they meant September the 8th!).
For more information on that issue, and an example of how to read the value safely, see the FAQ article Why are my dates not working properly?.
Click Here to Expand Forum to Full Width