VB icon

Convert SQL To HTML Table

Email
Submitted on: 1/5/2015 11:12:00 PM
By: Ron de Frates (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: ASP (Active Server Pages)
Views: 2046
 
     Returns the results of a SQL query as an HTML table. This small function is not only very fast it also simplifies the programming logic for displaying data.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Convert SQL To HTML Table
' Description:Returns the results of a SQL query as an HTML table. This small function is not only very fast it also simplifies the programming logic for displaying data.
' By: Ron de Frates (from psc cd)
'
' Inputs:SQL query
'
' Returns:A string containing the results as a HTML table
'
' Assumes:Requires ADO 2.0 or higher. I benchmarked this code against a traditional "rs.MoveNext" method and got a 100% speed improvement for 200 records.
'**************************************

<%
' ----------------------------------------------------------------------------
' NAME:cnvrtRSToHTML 
' AUTHOR: Ron de Frates
' DATE:01/21/2002
' PURPOSE: Converts recordset results to HTML table 
' LANGUAGE:ASP, HTML
' OBJECTS: ADODB.Connection 
' DATABASES:DSN_Admin
' ----------------------------------------------------------------------------
Function cnvrtRSToHTML(sSQL)
	Dim dbConn, rsSrc, sHTMLTbl
	
	Set dbConn = Server.CreateObject("ADODB.Connection")
	dbConn.Open
	
	Set rsSrc = dbConn.Execute(sSQL)
	
	sHTMLTbl = rsSrc.GetString(,,"</td><td>","</td></tr><tr><td>"," ") 
	sHTMLTbl = Mid(sHTMLTbl, 1, Len(sHTMLTbl) - 8) 
	sHTMLTbl = "<table border=1><tr><td>" & sHTMLTbl & "</table>"	
	cnvrtRSToHTML = sHTMLTbl
End Function
' -----------------------------------------------------------------------------
' ***** HOW TO CALL THIS FUNCTION *****
Dim sSQL
sSQL = "SELECT * FROM myTable " 
		
Response.Write(cnvrtRSToHTML(sSQL))
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.