By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

How This Code Works

P: n/a
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.

Apr 11 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
rn**@rediffmail.com wrote on 11 Apr 2007 14:05:32 -0700:
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
Apr 12 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.