Results 1 to 6 of 6

Thread: Date validation

Threaded View

  1. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Date validation

    Quote Originally Posted by annemccallum
    Hi
    can anyone tell me how to validate a date. I want the user to enter the date in the format 01/01/05 as opposed to 01.01.05 or 01-01-05. Alternatively can I set the text box to display the "/" in the appropriate position.

    thanks
    Anne
    Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4. On Error GoTo Text1_KeyPress_Error
    5.  
    6.     If (KeyAscii = 45) Or (KeyAscii = 46) Or (KeyAscii = 92) Then
    7.                 '45 = -
    8.                 '46 = .
    9.                 '92 = \
    10.                
    11.         KeyAscii = 47 ' /
    12.     ElseIf (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then ' Allows only Numbers
    13.         KeyAscii = 0
    14.     End If
    15.  
    16. On Error GoTo 0
    17. Exit Sub
    18.  
    19. Text1_KeyPress_Error:
    20.  
    21. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    22. End Sub
    23.  
    24. Private Sub Text1_LostFocus()
    25. On Error GoTo Text1_LostFocus_Error
    26. Dim strDateText As String
    27.  
    28.     strDateText = Me.Text1.Text
    29.    
    30.     If strDateText & "" = "" Then ' Check for a Null, or Empty String Values
    31.         Exit Sub
    32.     Else
    33.         strDateText = FormatDate(strDateText)
    34.        
    35.             If strDateText = "Invalid Entry" Then
    36.                'Display Error in Messagebox
    37.                 MsgBox "You Have entered an invalid date (" & Me.Text1.Text & ") , Please re-enter", vbInformation, "Invalid Date"
    38.                 Me.Text1.Text = "" ' Clears textbox
    39.                 Me.Text1.SetFocus  'Sets thje focus back to the textbox
    40.             Else
    41.                 Me.Text1.Text = strDateText
    42.             End If
    43.     End If
    44. On Error GoTo 0
    45. Exit Sub
    46.  
    47. Text1_LostFocus_Error:
    48. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_LostFocus of " & Me.Name
    49. End Sub
    50.  
    51. Private Function FormatDate(strDate As String) As String
    52. On Error GoTo FormatDate_Error
    53. Dim strDateText As String
    54.  
    55.     If IsDate(strDate) Then ' Checks if strDate contain a valid date,
    56.                             'if so formats it MM/DD/YYYY
    57.                            
    58.         FormatDate = Format(strDate, "mm/dd/yyyy")
    59.         Exit Function
    60.     Else
    61.         If IsNumeric(strDate) Then
    62.             Select Case Len(strDate)
    63.                 Case 6 'Checks if numbers were in the following format: 123102
    64.                     strDateText = Left(strDate, 2) & "/"
    65.                     strDateText = strDateText & Mid(strDate, 3, 2) & "/"
    66.                     strDateText = strDateText & Mid(strDate, 5, 2)
    67.                 Case 8  'Checks if numbers were in the following format: 12312002
    68.                     strDateText = Left(strDate, 2) & "/"
    69.                     strDateText = strDateText & Mid(strDate, 3, 2) & "/"
    70.                     strDateText = strDateText & Mid(strDate, 5, 4)
    71.                 Case Else 'If it isn't in one of the above formats return an invalid entry entry
    72.                     strDateText = "Invalid Entry"
    73.             End Select
    74.        
    75.         Else
    76.             strDateText = "Invalid Entry"
    77.         End If
    78.        
    79.             If IsDate(strDateText) Then 'Checks to make sure dates like
    80.                                         '88/65/2002 are not returned
    81.                 FormatDate = Format(strDateText, "mm/dd/yyyy")
    82.             Else
    83.                 FormatDate = "Invalid Entry"
    84.             End If
    85.     End If
    86.  
    87. On Error GoTo 0
    88. Exit Function
    89.  
    90. FormatDate_Error:
    91.  
    92. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FormatDate of " & Me.Name
    93. End Function

    or if you only want the user to enter numbers only, change the Text1_KeyPress Event to:

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. On Error GoTo Text1_KeyPress_Error
    3.  
    4.     If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then ' Allows only Numbers
    5.         KeyAscii = 0
    6.     End If
    7.  
    8. On Error GoTo 0
    9. Exit Sub
    10.  
    11. Text1_KeyPress_Error:
    12.  
    13. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    14. End Sub

    I hope this is want you were looking for.
    Last edited by Mark Gambo; May 19th, 2005 at 09:00 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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