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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement broke!

Status
Not open for further replies.

stevemarsh99

Technical User
Aug 10, 2005
88
GB
Hey guys,

I have this page that is supposed to email someone based on a date that sits in a database. The code looks fine but when I run the page it ALWAYS comes back with the 'else' part of the if statement.....ARRRRRRHGGGHH! Any ideas?

(also taking for granted we could fix it, do you think the date would work if the hard coded date that I have used in the example would be replaced with asp code to dynamically display date date in a databse?)

*******************************************************

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/ealert.asp" -->
<%
Dim rs_ealert
Dim rs_ealert_numRows

Set rs_ealert = Server.CreateObject("ADODB.Recordset")
rs_ealert.ActiveConnection = MM_ealert_STRING
rs_ealert.Source = "SELECT * FROM tbl_ealert"
rs_ealert.CursorType = 0
rs_ealert.CursorLocation = 2
rs_ealert.LockType = 1
rs_ealert.Open()

rs_ealert_numRows = 0
%>


<% if 30/11/2005 = dateadd("d", date(), -1) then %>

<%

FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "WORKING!!"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>
<% else %>
<%
FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "Its the else of the 'if' statement"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>
<% end if %>


<% MM_EditAction = CStr(Request("URL"))
If (Request.QueryString <> "") Then
MM_EditAction = MM_EditAction & "?" & Request.QueryString
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>KHL Digital Magazine eAlerts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
-->
</style>
<link href="css/Main_page.css" rel="stylesheet" type="text/css">
</head>

<body>

</body>
</html>
<%
rs_ealert.Close()
Set rs_ealert = Nothing
%>
*******************************************************
 
This test will always fail - you're testing what you've just set!
Code:
           Set objCDO = Nothing
            [COLOR=red]RedirectURL = ""
            If RedirectURL <> "" then[/color]
                If Request.QueryString <> "" Then
                      response.redirect(RedirectURL & "?" & Request.QueryString)
                else
                    response.redirect(RedirectURL)
                end If
            end if %>

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
The bit steve is struggling with(at the moment ;-) ) is this line
Code:
<% if 30/11/2005 = dateadd("d", date(), -1) then %>
He might get it to work with this instead.
Code:
<%
strDateDD = Datepart("d", Date())
strDateMM = Datepart("m", Date())
strDateYYYY = Datepart("yyyy", Date())
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = "30/11/2005"
%>
<%
if strMyDate = strToday then
%>
strToday matches strMyDate
<%
else
%>
They don't match!!
<%
end if
%>

Cheech

[Peace][Pipe]
 
Sorry, my ESP seems to be down at present! OP said
The code looks fine but when I run the page it ALWAYS comes back with the 'else' part of the if statement..
and then shows code that sets a variable and immediately tests it! That test will always fail, so I assumed that was what he was talking about.
stevemarsh99, can you confirm which If is troubling you?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
When I run this I get 900 emails!!!

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/ealert.asp" -->
<%
Dim rs_ealert
Dim rs_ealert_numRows

Set rs_ealert = Server.CreateObject("ADODB.Recordset")
rs_ealert.ActiveConnection = MM_ealert_STRING
rs_ealert.Source = "SELECT * FROM tbl_ealert"
rs_ealert.CursorType = 0
rs_ealert.CursorLocation = 2
rs_ealert.LockType = 1
rs_ealert.Open()

rs_ealert_numRows = 0
%>


<%
strDateDD = Datepart("d", Date())
strDateMM = Datepart("m", Date())
strDateYYYY = Datepart("yyyy", Date())
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = "29/11/2005"
%>
<%
if strMyDate = strToday then
%>

<%
FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "WORKING!!"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>

<%
else
%>



<%
FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "Its the else of the 'if' statement"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>
<%
end if
%>


<% MM_EditAction = CStr(Request("URL"))
If (Request.QueryString <> "") Then
MM_EditAction = MM_EditAction & "?" & Request.QueryString
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>KHL Digital Magazine eAlerts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
-->
</style>
<link href="css/Main_page.css" rel="stylesheet" type="text/css">
</head>

<body>

</body>
</html>
<%
rs_ealert.Close()
Set rs_ealert = Nothing
%>
 
It appears that your page is redirecting to itself. As far as I know you can't directly add a querystring to a Redirect. Have a look here (towards the bottom):

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Right it works now! But how do state that if the date is yesterday then run the script?

I am guessing the issue here is that the date is generated in bits so you can just subtract 1 from the 'day' section?
 
Unless its the first of the month, change to
Code:
<%
strDateDD = Datepart("d", DateAdd("d", -1, Date()))
strDateMM = Datepart("m", DateAdd("d", -1, Date()))
strDateYYYY = Datepart("yyyy", DateAdd("d", -1, Date()))
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = "29/11/2005"
%>

Untested but you should get the gist

[Peace][Pipe]
 
would this work?

<%
strDateDD = Datepart("d", DateAdd("d", -1, Date()))
strDateMM = Datepart("m", DateAdd("d", -1, Date()))
strDateYYYY = Datepart("yyyy", DateAdd("d", -1, Date()))
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = (rs_ealert.Fields.Item("MAGDATE").Value)
%>
 
Also, the -1 doesnt work because if i want it to inform someone on the last day of the month, if the dat after is present it fails?
 
Please explain. I dont understand your last comment?

[Peace][Pipe]
 
I mean that if you just subtract 1 from the date, (Day for example) and the date was the 1st of the month, what would happen. Like today, its the 30th, in my database i put in 01/12/2005 and it does the 'else' part of the statement even though it should do the 1st section?
 
This is it:

<%
strDateDD = Datepart("d", DateAdd("d", -1, Date()))
strDateMM = Datepart("m", DateAdd("d", -1, Date()))
strDateYYYY = Datepart("yyyy", DateAdd("d", -1, Date()))
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = (rs_ealert.Fields.Item("MAGDATE").value)

***01/12/2005 in the db***

%>
<%
if strMyDate = strToday then
%>


and it will always do the ELSE part of the if statement (the latter part) instead of doing the the 1st part...
 
If you are running that today then the comaprison is going to be
if 29/11/2005 = 01/12/2005 then
so it will do the else condition until the strToday is the 02/12/2005, now if what you want is take a date from the database and if today is the day before that date then just remove the - from the dateAdd things

[Peace][Pipe]
 
It still does the else!!!! AAARRRRGGGHHHH! I have even changed the -1 to 0's now and the dates in the database are today!! I have officially pulled all my hair out!

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/ealert.asp" -->
<%
Dim rs_ealert
Dim rs_ealert_numRows

Set rs_ealert = Server.CreateObject("ADODB.Recordset")
rs_ealert.ActiveConnection = MM_ealert_STRING
rs_ealert.Source = "SELECT * FROM tbl_ealert"
rs_ealert.CursorType = 0
rs_ealert.CursorLocation = 2
rs_ealert.LockType = 1
rs_ealert.Open()

rs_ealert_numRows = 0
%>


<%
strDateDD = Datepart("d", DateAdd("d", 0, Date()))
strDateMM = Datepart("m", DateAdd("d", 0, Date()))
strDateYYYY = Datepart("yyyy", DateAdd("d", 0, Date()))
strToday = strDateDD & "/" & strDateMM & "/" & strDateYYYY
strMyDate = (rs_ealert.Fields.Item("MAGDATE").value)
%>
<%
if strMyDate = strToday then
%>

<%
FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "WORKING!!"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>

<%
else
%>



<%
FormFields = "to~body~subject"
If FormFields <> "" Then
arrFormFields = split(FormFields,"~")
End If
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Circulation@eAlert.com"
objCDO.To = "marsh@khl.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "Its the else of the 'if' statement"
BodyString = Replace("Extra Hard coded MAIN BODY text","~",chr(13) & chr(10) )& chr(13)
If FormFields <> "" Then
For Each item In arrFormFields
BodyString = BodyString & item & ": " & Request.Form(item) & chr(13)
Next
End If
objCDO.Body = BodyString
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
Set objCDO = Nothing
RedirectURL = ""
If RedirectURL <> "" then
If Request.QueryString <> "" Then
response.redirect(RedirectURL & "?" & Request.QueryString)
else
response.redirect(RedirectURL)
end If
end if %>
<%
end if
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>KHL Digital Magazine eAlerts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
-->
</style>
<link href="css/Main_page.css" rel="stylesheet" type="text/css">
</head>

<body>

</body>
</html>
<%
rs_ealert.Close()
Set rs_ealert = Nothing
%>
 
Adding "0" is the same as Date() so not required. Try taking out the redirect & chucking in some response.writes so that you can see exactly what the 2 strings are.

[Peace][Pipe]
 
Great idea! I found out that the generated date (StrToday) creates today as:

2/12/2005

when the date it should be matching is:

02/12/2005

Anyway round this?
 
add this
Code:
strDateDD = Datepart("d", DateAdd("d", 0, Date()))
if LEN(strDateDD) = 1 then
strDateDD = "0" & strDateDD
end if

[Peace][Pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top