Reputation: 41
What would be the logic statement to notify the user that they purchased the same product within the last 90 days?
SQL = "SELECT id, PurchaseDate, ProductName from TableName where id = " & id)
Do Until objRS.EOF
if (objRS("ProductName") and objRS("PurchaseDate") <= dateadd(day,-90,getdate())) and objRS("ProductName") and isNull(objRS("PurchaseDate")) then
AlreadyPurchased = true
end if
objRS.movenext()
Loop
My loop that writes it to the page:
<%=objrs("ProductName")%><% if AlreadyPurchased then%>Last Purchased with last 90 days<%end if %>
Upvotes: 0
Views: 175
Reputation: 496
The best answer depends on a few assumptions:
A) your query does not return any NULL values, and
B) that you have previously stored the product name in a preexisting variable.
Your code has some issues.
1) if (objRS("ProductName")
is basically asking if the fieldset value is a True
boolean value. My first assumption is that you already know the product name that you're testing. So, the above snippet should be replaced with this: if (objRS("ProductName").value = strMyProductName
where strMyProductName
is a variable that stores the product name (as a string).
2) You should consider storing the calculated date in a variable outside the loop. No need to repeatedly compute the date if you can compute it once, store it in a variable, and use the variable in your comparison.
3) Remove the last component of your conditional and objRS("ProductName")
because it's redundant and has the same faulty logic as what I explained in (1) above.
4) Your DateAdd()
can be written better.
TL;DR
dim strMyProductName, dat90, AlreadyPurchased
AlreadyPurchased = false
strMyProductName = "Shoes"
dat90 = dateadd("d", -90, now) '90 days ago
Do Until objRS.EOF
if cstr(objRS("ProductName").value)=strMyProductName and cdate(objRS("PurchaseDate")) <= dat90 then
AlreadyPurchased = true
exit do
end if
objRS.movenext()
Loop
Upvotes: 2