How This Code Works

How This Code Works

am 11.04.2007 23:05:32 von 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 (...) in 7 different cells
(...). 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
%>


<%
Do Until(objRS.EOF)
%>

<%
For iCount=iCount To iCount+6
%>

<%
objRS.MoveNext
Next
%>

<%
Loop
%>
<%= objRS("Col1") %>

------------------------------------------------

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.

Re: How This Code Works

am 12.04.2007 09:32:37 von Daniel Crichton

rn5a@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 (...) in 7 different cells
> (...). 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
> %>
>


> <%
> Do Until(objRS.EOF)
> %>
>
> <%
> For iCount=iCount To iCount+6
> %>
>
> <%
> objRS.MoveNext
> Next
> %>
>
> <%
> Loop
> %>
>
<%= objRS("Col1") %>

> ------------------------------------------------
>
> 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
%>

<%
Do Until(objRS.EOF)
%>

<%
For iCount=iCount To iCount+6
%>

<%
Next
%>

<%
Loop

%>

<%
If Not objRS.EOF Then
Response.Write objRS("Col1")
objRS.MoveNext
End If
%>




Dan