How This Code Works

R

rn5a

I want an ASP app to retrieve records from a MS-Access database table
but display them in a HTML table but I want the HTML table table to
display only 7 records in a row (<tr>...</tr>) in 7 different cells
(<td>...</td>). In other words, if there are 14 records in the DB
table, the HTML table should display the 14 records in 2 rows - the
1st row displaying the 1st 7 records from the DB table & the 2nd row
displaying the remaining 7 records existing in the DB table. This is
how I did it:

------------------------------------------------
<%
On Error Resume Next

Dim objConn
objConn=Server.CreateObject("ADODB.CONNECTION")
'open the connection using ConnectionString

Dim strSQL
strSQL="SELECT * FROM MyTable ORDER BY Col1"

Dim objRS
Set objRS=Server.CreateObject("ADODB.RECORDSET")
objRS.Open strSQL,objConn

Dim iCount
%>
<table border=1>
<%
Do Until(objRS.EOF)
%>
<tr>
<%
For iCount=iCount To iCount+6
%>
<td><%= objRS("Col1") %></td>
<%
objRS.MoveNext
Next
%>
</tr>
<%
Loop
%>
</table>
------------------------------------------------

The above code does display the records how I want to display but to
be honest, I couldn't exactly understand the logic of the above code.
Can someone please explain me the logic behind the above code?

Also note the On Error Resume Next line. Though the above code
displays the records exactly how I want them to be displayed in the
HTML table, if I comment the On Error Resume Next line, then ASP
generates the

Exception occured.

error without pointing to any line. I know what's causing the error
but can't find a way out to overcome the error. Can someone please
help me resolve this error as well?

Thanks.
 
D

Daniel Crichton

I want an ASP app to retrieve records from a MS-Access database table
but display them in a HTML table but I want the HTML table table to
display only 7 records in a row (<tr>...</tr>) in 7 different cells
(<td>...</td>). In other words, if there are 14 records in the DB
table, the HTML table should display the 14 records in 2 rows - the
1st row displaying the 1st 7 records from the DB table & the 2nd row
displaying the remaining 7 records existing in the DB table. This is
how I did it:

------------------------------------------------
<%
On Error Resume Next

Dim objConn
objConn=Server.CreateObject("ADODB.CONNECTION")
'open the connection using ConnectionString

Dim strSQL
strSQL="SELECT * FROM MyTable ORDER BY Col1"

Dim objRS
Set objRS=Server.CreateObject("ADODB.RECORDSET")
objRS.Open strSQL,objConn

Dim iCount
%>
<table border=1>
<%
Do Until(objRS.EOF)
%>
<tr>
<%
For iCount=iCount To iCount+6
%>
<td><%= objRS("Col1") %></td>
<%
objRS.MoveNext
Next
%>
</tr>
<%
Loop
%>
</table>
------------------------------------------------

The above code does display the records how I want to display but to
be honest, I couldn't exactly understand the logic of the above code.
Can someone please explain me the logic behind the above code?

It's pretty simple - get the results, and for every 7 rows in the recordset
write them out in TD tags. First time round the For Next loop iCount will
increment from 0 to 6, second time round from 7 to 13, etc. although the
value of iCount is meaningless - it could just as easily have been "For
iCount = 1 To 7" and it would do the same job.

Also note the On Error Resume Next line. Though the above code
displays the records exactly how I want them to be displayed in the
HTML table, if I comment the On Error Resume Next line, then ASP
generates the

Exception occured.

error without pointing to any line. I know what's causing the error
but can't find a way out to overcome the error. Can someone please
help me resolve this error as well?

If the number of rows in the result is less than a multiplication of 7 (ie.
7, 14, 21, etc) then the MoveNext will cause an error because there is no
checking for EOF in the For Next loop. The On Error Resume Next disguises
this, and allows the creation of empty cells by simply ignoring the error
caused by = objRS("Col1") when the recordset has passed the last row, and
subsequently by the MoveNext because the recordset is already past the last
row so there is no row to move to. The following edit should handle this
without the need for the On Error statement

<%
Dim objConn
objConn=Server.CreateObject("ADODB.CONNECTION")
'open the connection using ConnectionString

Dim strSQL
strSQL="SELECT * FROM MyTable ORDER BY Col1"

Dim objRS
Set objRS=Server.CreateObject("ADODB.RECORDSET")
objRS.Open strSQL,objConn

Dim iCount
%>
<table border=1>
<%
Do Until(objRS.EOF)
%>
<tr>
<%
For iCount=iCount To iCount+6
%>
<td>
<%
If Not objRS.EOF Then
Response.Write objRS("Col1")
objRS.MoveNext
End If
%>
</td>
<%
Next
%>
</tr>
<%
Loop

%>
</table>



Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top