Welcome to Dream.In.Code
Become a VB Expert!

Join 137,261 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,559 people online right now. Registration is fast and FREE... Join Now!




Date validation code VBA

 
Reply to this topicStart new topic

Date validation code VBA, Trouble with Homework

obcran
13 Dec, 2006 - 03:28 PM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 3


My Contributions
I am new to VBA and I am doing a homework assignment. To make a date validation routine with vba. The problem is when I enter a wrong date msgbox says invaild day and then says date good, i need it to go back to validate the date. How can I do this?


Attached File(s)
Attached File  newest.doc ( 27.5k ) Number of downloads: 166
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Date Validation Code VBA
13 Dec, 2006 - 06:21 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
Trick #1

see the watermark on the bacground when you enter your comments?
you need to post your code like this

start with the bracket "[", type the word "code" and then type the close bracket "]"

this begins a code formated procedure where your code will display properly as code.

when you finnish entering your code, type the bracket "[" then a slash "/" then the word "code" followed by a close bracket "]"

this creates the look we want and need and eliminates the possibility of spreading unwanted viruses or trojans. like this.

CODE

Private Sub Form_Load()
    Private Sub txtYYYY_Exit(Cancel As Integer)
    ' This is where the VBA code for the date validation routine goes
    
Validate_Month:     '2100
        If Forms!frmDateChk.txtMM < "01" Then
            'MsgBox "Invalid Month Entered"
            GoTo Display_Bad
        Else
            GoTo Display_Good
        End If
            GoTo Exit_Rtn
        If Forms!frmDateChk.txtMM > "12" Then
            'MsgBox "Invalid Month Entered"
            GoTo Display_Bad
        Else
            GoTo Display_Good
        End If
            GoTo Exit_Rtn
      
        
    '2200_Validate_DD
    '2210_Validate_Feb_DD
    '2211_Validate_NonLeap_Feb_DD
        If Forms!frmDateChk.txtMM = "02" Then
            If CDec((Forms!frmDateChk.txtYYYY) / 4) - CInt((Forms!frmDateChk.txtYYYY) / 4) <> 0 Then
                'MsgBox "non leapyear"
                If (Forms!frmDateChk.txtDD < "01" Or Forms!frmDateChk.txtDD > "28") Then
                    'MsgBox "Invalid Feb Date entered for Non-Leap Year"
                    GoTo Display_Bad
                Else
                    GoTo Display_Good
                            
                    GoTo Exit_Rtn
                End If
            End If
        End If
                            
    '2212_Validate_Leap_Feb_DD
        If Forms!frmDateChk.txtMM = "02" Then
            If CDec((Forms!frmDateChk.txtYYYY) / 4) - CInt((Forms!frmDateChk.txtYYYY) / 4) = 0 Then
                'MsgBox "leap year"
                If (Forms!frmDateChk.txtDD < "01" Or Forms!frmDateChk.txtDD > "29") Then
                    'MsgBox "Invalid Feb Date entered for Leap Year"
                    GoTo Display_Bad
                Else
                    GoTo Display_Good
                End If
                GoTo Exit_Rtn
            End If
        End If
                
    '2220_Validate_Apr_Jun_Sep_Nov
        If (Forms!frmDateChk.txtMM = "04" Or_
            Forms!frmDateChk.txtMM = "06" Or_
            Forms!frmDateChk.txtMM = "09" Or_
            Forms!frmDateChk.txtMM = "11") Then
            'MsgBox "Apr Jun Sep or Nov"
            If (Forms!frmDateChk.txtDD < "01" Or_
                Forms!frmDateChk.txtDD > "30") Then
                GoTo Display_Bad
            Else
                GoTo Display_Good
            End If
            GoTo Exit_Rtn
            'enter logic to check DD here
        End If
    '2230_Validate_Other_Dd
        If Forms!frmDateChk.txtMM = "01" Or_
           Forms!frmDateChk.txtMM = "03" Or_
           Forms!frmDateChk.txtMM = "05" Or_
           Forms!frmDateChk.txtMM = "07" Or_
           Forms!frmDateChk.txtMM = "08" Or_
           Forms!frmDateChk.txtMM = "10" Or_
           Forms!frmDateChk.txtMM = "12" Then
            'MsgBox "Jan Mar May Jul Aug Oct or Dec"
            If (Forms!frmDateChk.txtDD < "01" Or_
                Forms!frmDateChk.txtDD > "31") Then
                GoTo Display_Bad
            Else
                GoTo Display_Good
            End If
            GoTo Exit_Rtn
            
            'enter logic to check DD
            
        End If
    
    '2300_Validate_YYYY
        If Forms!frmDateChk.txtYYYY < "1900" Or_
           Forms!frmDateChk.txtYYYY > "9999" Then
            MsgBox "Invalid Year"
            GoTo Display_Bad
        Else
            GoTo Display_Good
        End If
    
    '3000_Display_Response
    
Display_Good:     '3100
        MsgBox "Congratulations! The DATE is good!"
        GoTo Exit_Rtn
        
Display_Bad:     '3200
        MsgBox "Too bad! The DATE is NO good!"
        GoTo Validate_Month
    
Exit_Rtn:
        MsgBox "all done!"
End Sub


Private Sub CmdClose_Click()
    On Error GoTo Err_CmdClose_Click
    
        DoCmd.Close
    
Exit_CmdClose_Click:
        Exit Sub
    
Err_CmdClose_Click:
        MsgBox Err.Description
        Resume Exit_CmdClose_Click
        
    End Sub
End Sub



ok?

All I've done here is realign your code so that it is more readable.
You should only TAB OVER on each new procedure, not on every line.

for instance
CODE

IF condition THEN
    'indent all within this IF procedure to here and beyond only,
    IF newcondition THEN
         ' indent all newcondition related code here
        Print ResultsA
    ELSE
        ' we know this belongs only to newcondition
        Print ReslutsB
    END IF ' all multiline IF procedures must be ENDED
    'we can continue with code related to condition only
    WHILE (NOT .EOF)
        'this is a new procedure within condition still - a sibling of new-condition
         Update condition
         IF condition is TRUE THEN PRINT condition
             ' this is a one line IF Statement, no need to END IF or indent, except remarks
         GET nextRecord
     WEND
END IF


See how easy it is to read and find problems?

I have never worked with VBA but if it follows VB very closely, I think you need to seperate your SUBROUTINES

you Have all your code executing within the form load EVENT. Is this your intent?

Try looking at your code as I formatted it and see if you can fix the problem. Let me know.
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Date Validation Code VBA
13 Dec, 2006 - 06:42 PM
Post #3

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
I think I would begin in creating subs for each routine and calling each sub as needed i.e.

CODE

Private Sub Form_Load()
    Validate_Month
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub txtYYYY_Exit(Cancel As Integer)
    ' This is where the VBA code for the date validation routine goes
    
    
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub Validate_Month():   '2100
                If Forms!frmDateChk.txtMM < "01" Then
            'MsgBox "Invalid Month Entered"
            Display_Bad
                Else
            Display_Good
                End If
                Exit_Rtn
                If Forms!frmDateChk.txtMM > "12" Then
        'MsgBox "Invalid Month Entered"
        Display_Bad
    Else
        Display_Good
    End If
    Exit_Rtn
      
        
    '2200_Validate_DD
    '2210_Validate_Feb_DD
    '2211_Validate_NonLeap_Feb_DD
    If Forms!frmDateChk.txtMM = "02" Then
        If CDec((Forms!frmDateChk.txtYYYY) / 4) - _
        CInt((Forms!frmDateChk.txtYYYY) / 4) <> 0 Then
            'MsgBox "non leapyear"
            If (Forms!frmDateChk.txtDD < "01" Or _
            Forms!frmDateChk.txtDD > "28") Then
                'MsgBox "Invalid Feb Date entered for Non-Leap Year"
                Display_Bad
            Else
                Display_Good
                            
                Exit_Rtn
            End If
        End If
    End If
                            
    '2212_Validate_Leap_Feb_DD
    If Forms!frmDateChk.txtMM = "02" Then
        If CDec((Forms!frmDateChk.txtYYYY) / 4) - _
        CInt((Forms!frmDateChk.txtYYYY) / 4) = 0 Then
            'MsgBox "leap year"
            If (Forms!frmDateChk.txtDD < "01" Or _
                            Forms!frmDateChk.txtDD > "29") Then
                               'MsgBox "Invalid Feb Date entered for Leap Year"
                                Display_Bad
            Else
                Display_Good
            End If
            Exit_Rtn
        End If
    End If
                
    '2220_Validate_Apr_Jun_Sep_Nov
    '(When building a condition with many elements, seperate them with an indent)
    If (Forms!frmDateChk.txtMM = "04" Or _
                    Forms!frmDateChk.txtMM = "06" Or _
        Forms!frmDateChk.txtMM = "09" Or _
        Forms!frmDateChk.txtMM = "11") Then '(always indent AFTER A THEN)
            'MsgBox "Apr Jun Sep or Nov"
            If (Forms!frmDateChk.txtDD < "01" Or _
                Forms!frmDateChk.txtDD > "30") Then
                Display_Bad
            Else
                Display_Good
            End If
        Exit_Rtn
        'enter logic to check DD here
            
    End If
    '2230_Validate_Other_Dd
    If Forms!frmDateChk.txtMM = "01" Or _
        Forms!frmDateChk.txtMM = "03" Or _
        Forms!frmDateChk.txtMM = "05" Or _
        Forms!frmDateChk.txtMM = "07" Or _
                    Forms!frmDateChk.txtMM = "08" Or _
        Forms!frmDateChk.txtMM = "10" Or _
        Forms!frmDateChk.txtMM = "12" Then
            'MsgBox "Jan Mar May Jul Aug Oct or Dec"
            If (Forms!frmDateChk.txtDD < "01" Or Forms!frmDateChk.txtDD > "31") Then
                Display_Bad
            Else
                Display_Good
            End If
         Exit_Rtn
            
        'enter logic to check DD
            
    End If
    
    '2300_Validate_YYYY
    If Forms!frmDateChk.txtYYYY < "1900" Or _
        Forms!frmDateChk.txtYYYY > "9999" Then
            MsgBox "Invalid Year"
            Display_Bad
     Else
        Display_Good
    End If
    
    '3000_Display_Response
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub Display_Good():   '3100
        MsgBox "Congratulations! The DATE is good!"
        Exit_Rtn
End Sub
Private Sub Display_Bad():   '3200
        MsgBox "Too bad! The DATE is NO good!"
        Validate_Month
        
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub All_Done()
        MsgBox "all done!"
        
End Sub
'-------------------------------------------------------------------------------------------------------------    
Private Sub CmdClose_Click()
    On Error GoTo Err_CmdClose_Click
    DoCmd.Close
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub Exit_CmdClose_Click():
        Exit Sub
End Sub
'-------------------------------------------------------------------------------------------------------------
Private Sub Err_CmdClose_Click():
        MsgBox Err.Description
        Resume Exit_CmdClose_Click
End Sub


Like that

This post has been edited by KeyWiz: 13 Dec, 2006 - 06:49 PM
User is offlineProfile CardPM
+Quote Post

obcran
RE: Date Validation Code VBA
15 Dec, 2006 - 06:47 PM
Post #4

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 3


My Contributions
Thanks for the help. I got it ti work by adding another exit routine for good as well as the one for bad. Tahnks again.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 06:50PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month