Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBuild an HTML table from a database
Keywordsdatabase, HTML
CategoriesDatabase
 
Open the HTML file and write HTML header tags. Select records into a Recordset. Loop through the Fields collection to build headers for the table's columns.

Next loop through the records. For each record, loop through the Fields collection using the values to make HTML table entries.

Finish by writing HTML tags to close the table and the document.

 
' Create the HTML table representation.
Private Sub Command1_Click()
Dim fnum As Integer
Dim db As Database
Dim rs As Recordset
Dim num_fields As Integer
Dim i As Integer
Dim num_processed As Integer

    On Error GoTo MiscError

    ' Open the output file.
    fnum = FreeFile
    Open txtHTMLFile.Text For Output As fnum

    ' Write the HTML header information.
    Print #fnum, "<HTML>"
    Print #fnum, "<HEAD>"
    Print #fnum, "<TITLE>This is the title</TITLE>"
    Print #fnum, "</HEAD>"

    Print #fnum, ""
    Print #fnum, "<BODY TEXT=#000000 BGCOLOR=#CCCCCC>"
    Print #fnum, "<H1>Book Data</H1>"

    ' Start the HTML table.
    Print #fnum, "<TABLE WIDTH=100% CELLPADDING=2 " & _
        "CELLSPACING=2 BGCOLOR=#00C0FF BORDER=1>"

    ' Open the database.
    Set db = OpenDatabase(txtDatabase.Text)

    ' Open the recordset.
    Set rs = db.OpenRecordset( _
        "SELECT * FROM Books ORDER BY Title")

    ' Use the field names as table column headers.
    Print #fnum, "    <TR>"     ' Start a row.
    num_fields = rs.Fields.Count
    For i = 0 To num_fields - 1
        Print #fnum, "        <TH>";
        Print #fnum, rs.Fields(i).Name;
        Print #fnum, "</TH>"
    Next i
    Print #fnum, "    </TR>"

    ' Process the records.
    Do While Not rs.EOF
        num_processed = num_processed + 1
        ' Start a new row for this record.
        Print #fnum, "    <TR>";

        For i = 0 To num_fields - 1
            Print #fnum, "        <TD>";
            Print #fnum, rs.Fields(i).Value;
            Print #fnum, "</TD>"
        Next i
        Print #fnum, "</TR>";

        rs.MoveNext
    Loop

    ' Finish the table.
    Print #fnum, "</TABLE>"
    Print #fnum, "<P>"
    Print #fnum, "<H3>" & _
        Format$(num_processed) & _
        " records displayed.</H3>"
    Print #fnum, "<HR COLOR=C000C0>"
    Print #fnum, "Thanks to <A " & _
        "HREF=http://www.vb-helper.com>VB Helper</A>."

    Print #fnum, "</BODY>"
    Print #fnum, "</HTML>"

    ' Close the file and database.
    rs.Close
    db.Close
    Close fnum
    MsgBox "Processed " & _
        Format$(num_processed) & " records."

    Exit Sub

MiscError:
    MsgBox "Error " & Err.Number & _
        vbCrLf & Err.Description
End Sub
 
An enhanced version could use information you know about the database to provide a more customized result. For instance, this database contains book titles and corresponding URLs. The program displays these in separate columns in the table. You could make the titles be links to the URLs and not include the URL column in the table.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated