PDA

View Full Version : VBA Date Syntax



theloadstar
01-28-2002, 09:13 PM
Hello Support,
I have a quick and simple question for you pro's. I have looked through help files etc but as i don't know the exact name of what I am looking for, my efforts are of no use.
Basicly I am wanting to generate and Group ID number (in a text box) generated from the date of birth of the person i have in mt database. I have read and seen that the date (which i have as a medium-date) needs to be converted before it can be compaired. Does anyone know...
1. The convert to date command that i need (if it is needed)
2. The Syntax needed for this calculation (wether an Case Select or an If Then)

What i am looking for is something like....

If Date_of_birth Is Between 1/1/80 and 1/1/82 then Date_group_Id = 1
If Date_of_birth Is Between 1/1/82 etc


Thankyou for your time and help.
Dale

cprasley
02-14-2002, 11:57 PM
There is a CDate variable type in VBA. You can check the validity of the input string using IsDate() and then if it's valid use CDate() to convert to the date type. Once the data is in CDate format you can compare using <,=,>.

If IsDate(DOB) Then
**CDOB = CDate(DOB)
**If CDOB >= CDate("1/1/80") and CDOB < CDate("1/1/82") Then
****Date_Group_Id = 1
**ElseIf CDOB >= CDate("1/1/82") and CDOB < CDate("1/1/84") Then
****Date_Group_Id = 2
....

The actual format of the date strings will depend on the settings in your Windows Control Panel.