Creating the class
I'm assuming you have heard of classes in vbscript and know a little about
OOP. Lets have a look at the class, discuss it and then outline some possible
improvements:-
<%
Class caDataGrid
'private variables
private pAutoColumns, pConnStr, pSqlStr, intColCnt
Private pOutPut, pConn, pRec, x, y, pArray
'this runs when you create a reference to the caDataGrid class
Private Sub Class_Initialize()
Set pConn = server.createobject("adodb.connection")
Set pRec = server.createobject("adodb.recordset")
intColCnt = 0
pAutoColumns = True
End Sub
'Properties - all writable
Public Property Let ConnectionString(strConn)
pConnStr = strConn
End Property
Public Property Let AutoColumns(bAutoCols)
If bAutoCols = True or bAutoCols = False then
pAutoColumns = bAutoCols
End IF
End Property
Public Property Let SqlString(strSql)
pSqlStr = strSql
End Property
'Methods for our class
Public Sub AddColumn(strColName)
If intColCnt = 0 then
pOutPut = "<table width='100%' border=1
cellpadding=0 cellspacing=0>" & vbcrlf
pOutPut = pOutPut & "<tr>" & vbcrlf
End If
pOutPut = pOutPut & "<td><strong>" & strColName
& "</strong></td>" & vbcrlf
intColCnt = intColCnt + 1
End Sub
Public Sub Bind
pConn.Open pConnStr
Set pRec = pConn.Execute(pSqlStr)
If pAutoColumns = True then
'assign column names from returned recordset
pOutPut = "<table width='100%' border=1
cellpadding=0 cellspacing=0>" & vbcrlf
pOutPut = pOutPut & "<tr>" & vbcrlf
Redim pColNames(pRec.Fields.Count)
For x = 0 to pRec.Fields.Count - 1
pOutPut = pOutPut & "<td>"
& pRec.Fields(x).Name & "</td>" & vbcrlf
Next
End If
pOutPut = pOutPut & "</tr>" & vbcrlf
pArray = pRec.GetRows
For x = 0 to UBound(pArray, 2)
pOutPut = pOutPut & "<tr>" & vbcrlf
For y = 0 to UBound(pArray, 1)
pOutPut = pOutPut & "<td>" & pArray(y, x) & "</td>"
& vbcrlf
Next
pOutPut = pOutPut & "</tr>" &
vbcrlf
Next
pOutPut = pOutPut & "</table>" & vbcrlf
Response.Write pOutPut
End Sub
'this runs when we destroy our reference to caDataGrid
Private Sub Class_Terminate()
pOutPut = ""
pRec.Close
Set pRec = nothing
pconn.close
Set pConn = nothing
End Sub
End Class
%>