PDA

View Full Version : compare to previous record with IF statement


scroots
11-24-2002, 05:38 PM
i have a database and i which to use an IF statement to see if the value of the current record field is the same as the previous record field and if it is write some text.


thanks
scroots

whammy
11-24-2002, 08:24 PM
Hmm... actually what are you trying to do? There might be a better way to do it.

scroots
11-24-2002, 08:42 PM
i have setup a very simple calender, which lists stuuf in a table

--------
DATE
--------
event 1
--------
DATE
--------
Event 2
--------

but if you have to events on the same day, it shows it like the following. example date 24/11/02
--------
24/11/02
--------
event 1
--------
24/11/02
--------
Event 2
--------

which is very confusing to the eye, so i would like to acomplish so that if the date already exists it will not write out the date before the second, third, fourth, fifth and so on events.

scroots

scroots
11-25-2002, 09:10 PM
does anybody know any code or a better way of doing it?

scroots

Roelf
11-26-2002, 06:26 AM
Try this:

before looping the recordset, initialize a variable

dim testDate as string

then start looping the recordset

while not rs.eof
first step: check if the date is different from the testDate variable, the first time it will be
if rs.fields("date") <> testDate then
' now write out the date
Response.Write rs.fields("date")
end if
'do the rest of your writing stuff

' just before the movenext statement, set the variable testDate equal to rs.flieds("date")
' so the next loop will only write the date if its different from the previous one

rs.movenext
wend

this is not tested code, but it should give you an idea how to work things out

scroots
11-26-2002, 06:23 PM
thank you roelf, that will help me so much in all my coding projects as it is a valuble piece of code.

thank you
i`ll post back if i have any problems.
cheers:thumbsup:
scroots

scroots
11-26-2002, 08:10 PM
my computer dislikes wend. the exact error message is
Microsoft VBScript compilation error '800a0400'

Expected statement

/ww3/calender.asp, line 212

wend
^

any quick solutions or do i need to post my code?

scroots

whammy
11-27-2002, 12:03 AM
Try

Do While NOT rs.EOF

rs.MoveNext
Loop

Although the while statement shouldn't be a problem either. Did you close all your other stuff like If/Then statements, etc.?

scroots
11-27-2002, 06:24 PM
i have checked all of my code, but i may have missed something as i find it hard to find mistakes.

i have attached my code in a text document as it is too long to fit in this box.

can anyone find a mistake?

thanks
scroots

Roelf
11-27-2002, 07:11 PM
you have combined the two methods, you start the loop with

Do While not rsGuestBook.EOF

'and then you end with

rsGuestbook.movenext
wend
loop

the correct syntax is

Do While condition
'Response.writes here
Loop

or

While condition
'Response.writes here
WEnd
(even the case is correct in this example)

choose either one of them, but dont put them together