Results 1 to 6 of 6

Thread: textbox validation with FORMAT

  1. #1

    Thread Starter
    Lively Member nfo1212's Avatar
    Join Date
    Aug 2001
    Location
    Near Boston, U.S.A.
    Posts
    82

    textbox validation with FORMAT

    Hi,

    I am just learning VB. I am trying to get this code to work:


    Private Sub txtDateOff_LostFocus()
    If txtDateOff.Text = Format(txtDateOff, "dd/mm/yy") Then
    Exit Sub
    Else
    txtDateOff.Text = ""
    MsgBox "Please enter Date in dd/mm/yy format. For example, 08/25/02."
    End If
    End Sub

    As you can probably see I am trying to have the user enter a date in a textbox and then only allow it to "stick" (and be submitted to the database) if they get the dd/mm/yy format right. Only problem is I can't get it right: When I type in, say, 08/25/01 or anything else, I get the "Pleas enter date" msgbox. I might have the FORMAT syntax wrong. Thanks a bunch for your help.
    Nick

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Use the masked edit control, which are great for dates.

    Alternatively, try something like (note use of validate event:
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2. If IsDate(Text1) Then
    3.     Text1.BackColor = &H80000005
    4.     Text1 = FormatDateTime(Text1, vbShortDate)
    5. Else
    6.     Cancel = True
    7.     Text1.BackColor = vbRed 'or Beachbum would recommend a msgbox right here
    8. End If
    9. End Sub

  3. #3
    Tygur
    Guest
    I think you switched the months and days in this line:
    If txtDateOff.Text = Format(txtDateOff, "dd/mm/yy") Then

    The code is looking for a "dd/mm/yy" date. You're giving it a "mm/dd/yy" date (08/25/01).

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Ok a few things
    1) Why not just use IsDate() to check if it is a valid date.
    2) Masked edit boxes suck sorry Nucleus
    3) Error msgs for wrong typing from users suck BIGtime. (damn u Nucleus spreading bad rumours ) Why not let them key date in 250802 or 25-08-02 or 25/08/02 or 25/8/2002 or whatever and work it out.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5

    Thread Starter
    Lively Member nfo1212's Avatar
    Join Date
    Aug 2001
    Location
    Near Boston, U.S.A.
    Posts
    82

    thanks

    Thanks all,

    Tygur, you were exactly right. Pretty observant! The code works now.

    Beachbum, I know what you're saying. I don't like the masked edit boxes either. Any advice on how I might do this simply with isdate ().

    Nick

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Originally posted by beachbum
    Hi
    Ok a few things
    1) Why not just use IsDate() to check if it is a valid date.
    2) Masked edit boxes suck sorry Nucleus
    3) Error msgs for wrong typing from users suck BIGtime. (damn u Nucleus spreading bad rumours ) Why not let them key date in 250802 or 25-08-02 or 25/08/02 or 25/8/2002 or whatever and work it out.
    Regards
    Stuart
    1) See my first reply
    2) Skilled use of a masked edit box allows the user to enter any data they want, the only difference is the user has a graphical guide as to what is expected of them. Consequently I think you can easily make a case for either approach, sorry Beachbum .
    3) Come on BeachBum fess up, you da king of Msgboxes. Also can you post some code showing how to convert 250802 or 25082 or say 8252 to a valid date?

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