Even more efficient is to use the GetRows method of the Recordset to populate
a 2 dimensional array. Then you no longer have to use up resources accessing
the Recordset. dim arTable
arTable = objRec.GetRows
objRec.Close
set objRec = nothing
You now have a 2 dimensional array but how is the recordset information stored
in this array?
arTable's 2 dimensions are like this: arTable(TotalCols, TotalRows). So if
my Recordset returned 10 records of the above fields it would be arTable(3, 10)
and you would need to go from arTable(0, 0) to arTable(2, 9) to retreive each
value and you must go through the TotalCols elements first, followed by the TotalRows
element. You don't know how many records you will return so you need to use the
UBound function to evaluate them:- dim tCols, tRows
tCols = UBound(arTable, 1)
tRows = UBound(arTable, 2)
With an array of more than one dimension you need to supply the UBound function
with a value to indicate which element you wish to total. As above we set tCols
to the total of the first dimension and tRows to the total of the second dimension.
With this information you can now loop through the records using nested For
Next loops:- <table>
<%
Dim x, y
For x = 0 to tRows
Response.Write "<tr>"
For y = 0 to tCols
Response.Write "<td>" & rTable(y, x) & "</td>"
Next
Response.Write "</tr>"
Next
%>
Not as simple as directly referring to field names in the recordset object but
faster and the code is hardly more complicated. So now wehave a good model which
we can move/encapsulate in a handy vbscript class.
Comments