Appendix B: Advanced Dynamic Tables

Another use of tables.

Active Server Pages allow you to do some pretty interesting things. Say you want to be able to view the contents of any table within your database dynamically. The following example requires 3 pages: table chooser, table viewer, record viewer:

Table Chooser

Source (frmChooser.asp):

<body>
<form method="POST" action="frmViewTbl.asp">
<table border="1">
<tr>
  <td>Choose a table from the following</td>
</tr>
<tr>
  <td>
    <select name="tbl" size="1">
      <option value="DAT_Customers">DAT_Customers</option>
      <option value="DAT_Orders">DAT_Orders</option>
      <option value="DAT_Suppliers">DAT_Suppliers</option>
      <option value="DAT_Shippers">DAT_Shippers</option>
    </select>
  </td>
</tr>
<tr>
  <td>
    <input type="submit" value="Submit" name="B1">
    <input type="reset" value="Reset" name="B2">
  </td>
</tr>
</table>
</form>
</body>

Sample output:

Choose a table from the following

Table Viewer

Source (frmTblView.asp):

<!--#include virtual="/includes/adovbs.inc"-->
<!--#include virtual="/includes/subConn.inc"-->
<%
sql = "SELECT * FROM DAT_Orders ORDER BY CustomerID"
openCN
rs.Open sql, cn
FieldCount=rs.fields.count-1
if FieldCount > 5 then
  FieldCount=4
end if
%>

<html>

<head>
<title></title>
</head>
<body>
<form action="frmChooser.asp" method="post">
<table border="1">
<tr>
<%for i=0 to FieldCount%>
<td><%=rs.fields(i).name%></td>
<%next%>


</tr>
<%
if rs.RecordCount > 10 then
    max=9
else
    max=(rs.RecordCount)-1
end if
for j=0 to max
%>

<tr>
<%for i=0 to FieldCount%>
<td><nobr><%if i = 0 then%>
<a href="frmFull.asp?tbl=<%=tbl%>&amp;rec=<%=rs.fields(i).value%>">
<%end if%><%=rs.fields(i).value%><%if i = 0 then%></a>
<%end if%></nobr></td>
<%next%>
</tr>
<%
    rs.MoveNext
    next
rs.Close
CloseCN
%>

<tr>
<td colspan="<%=FieldCount + 1%>">
  <input type="submit" value="Select Table">
</td>
</tr>
</table>
</form>
</body>
</html>

Sample output:

OrderID CustomerID EmployeeID OrderDate RequiredDate
10248 VINET 5 8/4/94 9/1/94
10249 TOMSP 6 8/5/94 9/16/94
10250 HANAR 4 8/8/94 9/5/94
10251 VICTE 3 8/8/94 9/5/94
10252 SUPRD 4 8/9/94 9/6/94
10253 HANAR 3 8/10/94 8/24/94
10254 CHOPS 5 8/11/94 9/8/94
10255 RICSU 9 8/12/94 9/9/94
10256 WELLI 3 8/15/94 9/12/94
10257 HILAA 4 8/16/94 9/13/94

Record Viewer

Source (frmRecView.asp):

<!--#include virtual="/includes/subConn.inc"-->
<%
tbl=request.querystring("tbl")
rec=request.querystring("rec")

openCN
'get first field name
sql = "SELECT * FROM " & tbl
rs.Open sql, cn
    fld=rs.fields(0).name
rs.Close

sql="SELECT * FROM " & tbl & " WHERE "

if isNumeric(rec) then
    sql=sql & fld & "=" & rec
else
    sql=sql & fld & "='" & rec & "'"
end if

rs.Open sql, cn, adOpenKeyset
FieldCount=rs.fields.count-1
%>

<body>
<form action="frmTblView.asp" method="post">
<input type="hidden" name="tbl" value="<%=tbl%>">
<table border="1">
<%for i=0 to FieldCount%>
<tr>
  <td align="right"><%=rs.fields(i).name & ":"%></td>
  <td><%=rs.fields(i).value%></td>
</tr>
<%
next
CloseCN
%>
<tr>
  <td colspan="2" align="center">
  <input type="button"
  onclick="window.location.href=&quot;frmChooser.asp&quot;"     value="Select Table">
  <input type="submit" value="View Table"></td>
</tr>
</table>
</form>
</body>

Sample output:

OrderID: 10257
CustomerID: HILAA
EmployeeID: 4
OrderDate: 8/16/94
RequiredDate: 9/13/94
ShippedDate: 8/22/94
ShipVia: 3
Freight: 81.91
ShipName: HILARI╙N-Abastos
ShipAddress: Carrera 22 con Ave. Carlos Soublette #8-35
ShipCity: San Crist≤bal
ShipRegion: Tßchira
ShipPostalCode: 5022
ShipCountry: Venezuela