If you have any tips to contribute, email us at tips@codeoftheweek.com. 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 we discuss how generate HTML for any database table or query.
If you have any questions about using this module, let us know at questions@codeoftheweek.com
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 asp@codeoftheweek.com
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).
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
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: http://www.codeoftheweek.com/membersonly/bi/0128.html