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

Determine if last record

Status
Not open for further replies.

Billybonga

IS-IT--Management
Jun 22, 2002
72
GB
Hi all,

I've scratching my head trying to figure this out and hope you can help.

I have voting script that displays the question in a file vote.asp?ID=X where X is the question from that database record. These records are processed in cast.asp?ID=X

I have a few things going on in this file
1) Input data to database and then progress (redirect) to next question so
Code:
 Response.Redirect "vote.asp?ID="&questionID+1

2) if the ANSWER ID is 44 then redirect to a certain question
Code:
 	if answerID = 44 then
		Response.Redirect "vote.asp?ID=3"
		response.end
	    end if

3) and this is where I'm simi stuck - If the question is the last QUESTION in the database then redirect to a thank you.

Now I can do this manually by

Code:
if questionID = 9 then 
Response.Redirect "thankyou.asp"

This works, but since I have an administration section when you can add extra questions question 9 won't alway be the last question/record in the database and I'd perfer not to manually edit the file each time I add a new question.

Can anyone explain how I can determine if a questionID is the last record in the database?

Thanks for you help

Billybong
 
Perhaps you can make another query using the SQL function named MAX() to find the largest question ID. Or a query sorted DESCENDING by question ID and then take a look at the TOP 1.
 
Can you post the code that retrieves the questions from the database?

You should be able to grab RecordCount and do something like the following:

Code:
if questionID = oRS.RecordCount() then 
Response.Redirect "thankyou.asp"

Where oRS is a record set.


[small]"There's an old saying in Tennessee — I know it's in Texas, probably in Tennessee — that says, fool me once, shame on — shame on you. Fool me — you can't get fooled again." - George W. Bush[/small]
<.
 
This is the code from the vote.asp page

As you see there is a line for if the question isn't found in the database.

Code:
<%

	' Thanks to Tom Holder, who developed the IP restriction,
	' "additional information" extension and ported graphical
	' display for visitors. 
	' Visit his site at [URL unfurl="true"]http://www.ymonda.co.uk/[/URL] for more
	' ASP source code and products.
	
	lAnketID = request("ID")

	set conn = Server.CreateObject("ADODB.Connection")
	Set rs = Server.CreateObject("ADODB.Recordset")
	conn.open sConnAnket
	set rs.ActiveConnection = conn
	rs.CursorType = adOpenStatic

	sSQL = "SELECT SurveyID, SurveyName, SurveyQuestion FROM Surveys WHERE SurveyID = " & lAnketID
	rs.Open sSQL, , , adCmdTable

	if rs.eof or rs.bof then
		response.write "Error : Survey not found<br>Survey ID:" & lAnketID
		response.end
	end if
	
	sSurveyName = rs("SurveyName")
	sSurveyQuestion = rs("SurveyQuestion")
	
	rs.close

%>


Here is cast.asp

Code:
  <%
	
	lAnketID = request("ID")
	lSecenekID = request("optSecenek")
	UserIP = Request.ServerVariables("REMOTE_ADDR")
	
	if lSecenekID = 0 then
		%>
                    </p>
                    <p class="smalltext"> <strong><font color="#000000">Please 
                      select an option.</font></strong><br>
                      <a href="<%=request("http_referer")%>">Go back</a> 
                      <%
		response.end
	end if
	
	set conn = Server.CreateObject("ADODB.Connection")
	Set rs = Server.CreateObject("ADODB.Recordset")
	conn.open sConnAnket
	set rs.ActiveConnection = conn
	rs.CursorType = adOpenStatic

	sSQL = "SELECT IP FROM Answers WHERE IP ='" & UserIP & "' AND SurveyID = " & lAnketID
	rs.Open sSQL, , , adCmdTable
	
	If rs.Eof then
	
		rs.close
		sSQL = "SELECT SurveyID, OptionID, IP FROM Answers WHERE AnswerID = 0"
		rs.Open sSQL, , , adCmdTable
	
		rs.AddNew
		rs("SurveyID") = lAnketID
		rs("OptionID") = lSecenekID
		rs("IP") = UserIP
		
		rs.update
		rs.close
		
		if lSecenekID = 44 then
		Response.Redirect "vote.asp?ID=3"
		response.end
	    end if
		
		if lAnketID = 8 then %>
		
		<table border="0" cellspacing="1" cellpadding="2" width="100%" bgcolor="#ffe4b5">
                      <tr> 
                        <td colspan=2 class="header2"><font color="#000000">Thank 
                          you for participating in our survey.</font></td>
                      </tr>
<& end if %>



Thanks to you both for your prompt replies

Bilybong
 
Sorry to sound so lame ... but I've really only started to program with asp, and MS Access databases.

I have tried completing a record set count but it fails with error:

Code:
Microsoft VBScript runtime (0x800A01C2)
Wrong number of arguments or invalid property assignment: 'rs.RecordCount'

I have also tried looking on how to do:

Or a query sorted DESCENDING by question ID and then take a look at the TOP 1.

but can't figure out how to do it :(

Certainly doing a sorted descending query would be the best way to go about it as, if a question is deleted (which it can do from the admin section) then the record count and the question id will no longer match.

Any help or advice is greatly appreciated.

Billybonga

 
Here's is what I have tried, but still isn't working:

Code:
	set conn = Server.CreateObject("ADODB.Connection")
	Set rs = Server.CreateObject("ADODB.Recordset")
	conn.open sConnAnket
	set rs.ActiveConnection = conn
	rs.CursorType = adOpenStatic


	sSQL = "SELECT SurveyID FROM Surveys WHERE SurveyID = (select max(SurveyID) from Surveys)"
	rs.Open sSQL, , , adCmdTable
	sLastID = rs("SurveyID")
	rs.close
				
	if lAnketID = sLastID then
	Response.Redirect "thankyou.asp"
	
	else		
		Response.Redirect "vote.asp?ID="&lAnketID+1
		end if

All that is happening is it casts the vote, then tries to forward me to the next question "vote.asp?ID="&lAnketID+1

I was thinking that I could put the Thank You into the vote.asp page where it has :

Code:
if rs.eof or rs.bof then
		response.write "Error : Survey not found<br>Survey ID:" & lAnketID
		response.end
	end if

but then if someone or something changes the address in the address bar it would make it display that they participated in the poll instead of the error "survey not found"

Thanks for all your help so far ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top