' Constants for standard HTML tags.
Const TABLE_START = "<TABLE BORDER=1>"
Const TABLE_END = "</TABLE>"
Const ROW_START = "<TR>"
Const ROW_END = "</TR>"
Const COLUMN_HEADER_START = "<TH>"
Const COLUMN_HEADER_END = "</TH>"
Const COLUMN_START = "<TD>"
Const COLUMN_END = "</TD>"
Function BuildTable(ByVal sSQL As String) As String
' Open data source using the OLE-DB Jet provider.
Dim conn As New ADODB.Connection
conn.Provider = "Microsoft.Jet.OLEDB.3.51"
conn.Properties("Data Source") = App.Path & _
"\Customers.mdb"
conn.Open
' open recordset
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseServer
rs.CursorType = adOpenForwardOnly
rs.LockType = adLockReadOnly
rs.Open sSQL, conn
' construct HTML table from ADO recordset
Dim sTable As String
sTable = TABLE_START
' put field names in top row
sTable = sTable & ROW_START
Dim flds As Fields, fld As Field
Set flds = rs.Fields
For Each fld In flds
sTable = sTable & COLUMN_HEADER_START & _
fld.Name & _
COLUMN_HEADER_END
Next fld
sTable = sTable & ROW_END
' add data to table
Do Until rs.EOF
' add a row in the table for each record
sTable = sTable & ROW_START
For Each fld In flds
sTable = sTable & COLUMN_START & _
FormatField(fld) & _
COLUMN_END
Next fld
sTable = sTable & ROW_END
rs.MoveNext
Loop
sTable = sTable & TABLE_END
' Explicitly close ADO connection.
rs.Close
conn.Close
Set conn = Nothing
' Return HTML table to caller.
BuildTable = sTable
End Function
' Utility function to format currency values.
Private Function FormatField(fld As Field) As String
Select Case fld.Type
Case adCurrency:
FormatField = Format(fld.Value, "$#,##0")
Case Else
FormatField = fld.Value
End Select
End Function
Figure 8 CmyWebComponent
Const PAGE_TITLE = "Welcome to the MIND Site"
Const PAGE_START = "<HTML><HEAD><TITLE>" & _
PAGE_TITLE & _
"</TITLE><BODY>"
Const PAGE_END = "</BODY></HTML>"
Sub Main()
Dim ObjCtx, rsp As Response
Set ObjCtx = GetObjectContext()
Set rsp = ObjCtx("Response")
' write page header
rsp.Write PAGE_START
' write page context
Dim sBody As String
sBody = "<H2>Customers Report</H2>"
Dim TableBuilder As CTableBuilder, sProgID As String
sProgID = "TableBuilder.CTableBuilder"
Set TableBuilder = ObjCtx.CreateInstance(sProgID)
Dim sSQL As String
sSQL = "SELECT * FROM Customers"
sBody = sBody & TableBuilder.BuildTable(sSQL)
rsp.Write sBody
' write page footer
rsp.Write PAGE_END
End Sub