Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing values not working 1

Status
Not open for further replies.

DebbieC

Programmer
Mar 29, 2001
168
0
0
US
I have a month and year that I am passing from another page and I want to compare it to the current date and even when they are the same it's not recognizing them as being the same.

Here's the code:
Code:
sMonth = Request.Form("Month")
sYear = Request.Form("Year")

CurrentMonth = Month(Date)
CurrentYear = Year(Date)

If CurrentMonth < 10 Then
	CurrentMonth = "0" & CurrentMonth
Else
End If


If sMonth < 10 and sYear = "2006" Then
	Response.Redirect("No.asp?Error=3")
Else
	If sMonth = CurrentMonth and sYear = CurrentYear Then
		Response.Redirect("No.asp?Error=4")
	Else
		If sMonth > CurrentMonth and sYear = CurrentYear Then
			Response.Redirect("No.asp?Error=4")
		Else
			Response.Redirect("BHSurveyResults.asp")
		End If			
	End If
End If

The first part that is hardcoded with the 10 and "2006" works but the part where it compares isn't. Everything that ends up in the first Else statement winds up going to the last Else statement because it doesn't see that it matches the others.
 
You're comparing a variant of subtype "string" (sYear) to a variant of subtype "date" (CurrentYear).

Try:
Code:
CurrentYear = Year(Date) & ""

 
A simpler solution would be to use the cInt() method to convert the string values from the Form to integer values, for comparison to the integer values returned from the Month() and Year() methods.
This is simpler because you won't need to worry about whether there is a preceding 0 or not on month values.

Additionally, you could simplify your bottom logical structure by using ElseIf statements rather than nesting If statements:
Code:
If sMonth < 10 and sYear = "2006" Then
    Response.Redirect("No.asp?Error=3")
ElseIf sMonth = CurrentMonth and sYear = CurrentYear Then
    Response.Redirect("No.asp?Error=4")
ElseIf sMonth > CurrentMonth and sYear = CurrentYear Then
    Response.Redirect("No.asp?Error=4")
Else
    Response.Redirect("BHSurveyResults.asp")
End If

-T

 
Thanks for all of your help. I had tried the cInt but it didn't work but I did it wrong. That is now working.

Thanks for rewriting the ElseIf statement for me. I sometimes forget that it's easier to do that.

Thanks again!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top