Visual Basic Code of the Week (COTW)
Issue #128
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Database Queries to HTML Tables

Extra CASH!

If you have any tips to contribute, email us at Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $25 US Dollars.


In this Issue

In this issue we discuss how generate HTML for any database table or query.

If you have any questions about using this module, let us know at


This class module is the start of a full-featured class to convert any ADO recordset into an HTML table which can be viewed in any browser. One of the best uses of this is to generate an ActiveX component with this class so you can call it from your Active Server Page scripts. Using this kind of technique allows you to provide your web site users with dynamic content. We are planning on using this to build a better search tool for all our back issues on the web.

To use this class you need to at least set the Recordset property and then call the HTML method to return the HTML..

If you are interested in seeing more ASP related topics, please email us at


Public TableBorderWidth As Integer

This allows you to specify the width of the table border as it appears in the browser. This value defaults to 1.

Public BackgroundGraphic As String

Allows you to specify a background graphic to enhance the looks of your page.

Public Property Set Recordset(RS As ADODB.Recordset)

Specifies the recordset to use to generate the HTML page.

Public Property Let PageTitle(sTitle As String)
Public Property Get PageTitle() As String

Title that will appear in the browser.

Public Property Get HTML() As String

Returns all the HTML text that is required to display the recordset specified by the Recordset property. If you call this from an ASP page you would typical just use the Reponse.Write command to send the text to the browser. You can also store it into a file for later use (as the sample shows).

Sample Usage

This sample shows one way to use the cADOtoHTML class. It assumes that you have made references to 'Microsoft ActiveX Data Objects 2.x Library' and have added the 'Microsoft Internet Controls' custom control to the project. It also assumes you have a form with two text boxes called txtSQL and txtDB; one button called cmdRun; one web browser control called web. This sample avoids error trapping to reduce the length of the source code.

Private Sub cmdRun_Click()
    Dim Conn As New ADODB.Connection
    Dim RS As New ADODB.Recordset
    Dim adohtml As New cADOtoHTML
    Dim iFile As Integer

    ' make connections with the access database
    With Conn
        .Provider = "Microsoft.JET.OLEDB.3.51"
        .CommandTimeout = 500
        .ConnectionTimeout = 500
        .Open txtDB.Text, "admin", ""
    End With
    RS.Open txtSQL.Text, Conn, adOpenStatic, adLockReadOnly

    ' assign the recordset created above to the adotohtml class
    ' and prepare the rest of the class for outputting the
    ' html text.
    Set adohtml.Recordset = RS
    adohtml.BackgroundGraphic = "e:\cotw\site\images\logo.gif"

    ' open a text file and get the html for the above recordset
    ' and save it to the text file
    iFile = FreeFile
    Open App.Path & "\temp.html" For Output As iFile
    Print #iFile, adohtml.HTML
    Close iFile

    ' display the text file in a browser window
    Call web.Navigate("file://" & App.Path & "\temp.html")
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945