Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Looping conditions


Message #1 by "David Bui" <dbui@b...> on Wed, 15 Jan 2003 12:02:01
Please can someone help me as I have been working on this problem for days 
with no success.

I have a combobox in HTML which posts a value to search an access 
database, then return the relevant results to an asp page.  Everything 
works fine, although the problem is that when the search returns no 
results, I would like to display a message on screen stating that there 
are no records found. The code is below;

Dim i
If Not oRS.EOF Then 
While Not oRS.EOF 
	For i = 1 to 8
		If Request.Form("degree") = oRS.Fields("Degree_" & i & "") 
Then  %>
			<%Response.Write vbTab & "<tr><td><b>" & vbCrLf%>
			<a href="returnUnit.asp?id=<%=oRS.fields.item
("ID").value%>">
			<%Response.Write ""& oRS.Fields("Unit_Title") 
& "<br>"%>
			</a>
	<%
	
	End If 
	Exit For
	Next 'i
	oRS.Movenext
	WEND
	Else
		Response.Write vbTab & "<tr><td><b>" & vbCrLf
		Response.Write"No Match"
End If

I have the code above already working on another similiar form, but I just 
can't get it to work with this one. So if anyone has any idea please 
please please reply.

Message #2 by "sashidhar" <sashi@a...> on Thu, 16 Jan 2003 09:31:46
u Got the answer? Or shall i give a sample!
Message #3 by "David Bui" <dbui@b...> on Thu, 16 Jan 2003 10:52:32
It's fine now, as I have found a solution to my problem. What I did was 
put a counter variable into the loop, if the variable remained at 0, then 
it displays a message. Here is the code below.

Dim i

Dim chkValue  'Value to write the result of each loop to
chkValue = 0
If NOT oRS.EOF Then 
	While Not oRS.EOF 
		For i = 1 to 8
			If oRS.Fields("Degree_" & i & "") = Request.Form
("degree") Then  %>
				<%Response.Write vbTab & "<tr><td><b>" & 
vbCrLf%>
				<a href="returnUnit.asp?id=<%
=oRS.fields.item("ID").value%>">
				<%Response.Write ""& oRS.Fields
("Unit_Title") & "<br>"%>
				</a>
				<%chkValue = chkValue + 1
				
				<%
			
			End If 
			Exit For
		Next 'i
		oRS.Movenext
	WEND
	If chkValue = "0" Then
				Response.Write "<b>" & "There were no 
units linked to the Degree you searched for. Please try another search." 
&"</b>"
	End If
End If

  Return to Index