Date validation code VBA

Trouble with Homework

Page 1 of 1

3 Replies - 21581 Views - Last Post: 15 December 2006 - 07:47 PM Rate Topic: -----

#1 obcran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 06

Date validation code VBA

Post icon  Posted 13 December 2006 - 04:28 PM

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)


Is This A Good Question/Topic? 0
  • +

Replies To: Date validation code VBA

#2 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Date validation code VBA

Posted 13 December 2006 - 07:21 PM

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.

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
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.
Was This Post Helpful? 0
  • +
  • -

#3 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Date validation code VBA

Posted 13 December 2006 - 07:42 PM

I think I would begin in creating subs for each routine and calling each sub as needed i.e.

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 December 2006 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 obcran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 06

Re: Date validation code VBA

Post icon  Posted 15 December 2006 - 07:47 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1