vb Loop through recordset

Dim conn
Dim rs
Dim x

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("ConnectionString")

If conn.state = 1 Then
	Set rs = Server.CreateObject("ADODB.recordset")
	
	rs.Open "Select * From myTable",conn,0,1
	
	If Not rs.BOF Then
		Response.Write "<table>"

		'loop through header row first
		For Each x In rs.Fields
			Response.Write "<td><strong>"
			Response.Write(x.name)
			Response.Write("</strong></td>") 
		Next

		'loop through each data row
		Do While Not rs.EOF
			Response.Write "<tr>"
			
			For Each x In rs.Fields
				Response.Write "<td>"
				Response.Write(x.value)
				Response.Write("</td>") 
			Next
			
			Response.Write("</tr>")
			
			rs.MoveNext
		Loop
		
		Response.Write "</table>"
	End If
		
	rs.close
	conn.close
End If
	
Set rs = nothing
Set conn = nothing
Loops through both the header and data rows in a recordset, writing them out after each iteration.

Updated: Tuesday 5th October 2010, 08:39pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.