Results 1 to 6 of 6

Thread: Date Validate

  1. #1

    Thread Starter
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193

    Date Validate

    Sorry again

    I am trying to stop the user putting in a age that is older than 25 and younger than 16 and am 3 combo boxes any ideas how i can validate it???


    Thanks all

    Sam Lad

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Can you post your code ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    What is the problem? I'm not sure of what the last part of your question (about 3 comboboxes) means, but you ought to be able to check the box during the On_Change event. If the first character typed is anything other than 1 or 2, the rest doesn't matter.

    I see that you would have the problem that the user could get away with only typing 1 or 2 and then going on. For this reason, it might be safer to put validation code later on, like when the user is leaving the form. You might try it in the LostFocus event, but I don't trust that one.

  4. #4

    Thread Starter
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193
    I have no code atm

    what i require is the user to tell me there date of birth
    they must
    select a year by using a combobox then
    to select a month then a day

    then i want my program to tell them there age in yrs

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Oh, I see.

    If you are using combo boxes, you will need to let the user enter all three of the parts of the date before you can do any validating. Therefore, I would suggest that you move any validation to a control button.

    You could do a rough check once they have entered the year to see whether or not their age could possibly fit the range you want, but if it is at the edge of the range, you would need to check the month and possibly the day to be sure.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim strBirthDate As String
    2.     Dim intMonths As Integer
    3.     Const UPPER_AGE = 300 ' 25 years
    4.     Const LOWER_AGE = 192 ' 16 years
    5.    
    6.     strBirthDate = cboYear.Text & "/" & Format$(cboMonth.Text, "00") & "/" & Format$(cboDay.Text, "00")
    7.     If Not IsDate(strBirthDate) Then
    8.         MsgBox "Invalid date"
    9.     End If
    10.    
    11.     intMonths = DateDiff("m", strBirthDate, Now())
    12.     If intMonths > UPPER_AGE Or intMonths < LOWER_AGE Then
    13.         MsgBox "Birdate out of range"
    14.     End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width