Analyse the tables and saved queries in an Access database with ADOX

5 (3 votes)

The following code will enumerate the tables and their properties, together with the queries in an Access database. Saved SELECT queries are referenced using the Views collection, and INSERT, UPDATE and DELETE queries are referenced from the Procedures collection.

<% 
Dim columnTypes(205) 
columnTypes(0) = "Empty" 
columnTypes(2) = "SmallInt" 
columnTypes(3) = "Long Integer" 
columnTypes(4) = "Real" 
columnTypes(5) = "Double" 
columnTypes(6) = "Currency" 
columnTypes(7) = "Date" 
columnTypes(8) = "BSTR" 
columnTypes(9) = "IDispatch" 
columnTypes(10) = "Error Code" 
columnTypes(11) = "Boolean" 
columnTypes(12) = "Variant" 
columnTypes(13) = "IUnknown" 
columnTypes(14) = "Decimal" 
columnTypes(16) = "TinyInt" 
columnTypes(17) = "Unsigned TinyInt (BYTE)" 
columnTypes(18) = "Unsigned Small Int (WORD)" 
columnTypes(19) = "Unsigned Int (DWORD)" 
columnTypes(20) = "BigInt" 
columnTypes(21) = "Unsigned Big Int" 
columnTypes(64) = "FileTime" 
columnTypes(72) = "Unique Identifier (GUID)" 
columnTypes(128) = "Binary" 
columnTypes(129) = "Char" 
columnTypes(130) = "nChar" 
columnTypes(131) = "Numeric" 
columnTypes(132) = "User Defined (UDT)" 
columnTypes(133) = "DBDate" 
columnTypes(134) = "DBTime" 
columnTypes(135) = "SmallDateTime" 
columnTypes(136) = "Chapter" 
columnTypes(138) = "Automation (PropVariant)" 
columnTypes(139) = "VarNumeric" 
columnTypes(200) = "VarChar" 
columnTypes(201) = "Text" 
columnTypes(202) = "Text" 
columnTypes(203) = "Memo" 
columnTypes(204) = "VarBinary" 
columnTypes(205) = "Image" 

Dim Connstr, adox, Conn, tbl
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.Mappath("NWind.mdb")

Set adox = CreateObject("ADOX.Catalog") 
Set Conn = CreateObject("ADODB.Connection") 
Conn.open ConnStr 
adox.activeConnection = Conn 
Response.Write "<table><tr><td width=""25%"" valign=""top"">"

Response.Write "<h2>Tables</h2>"

For Each tbl In adox.Tables
On Error Resume Next
  If tbl.Type = "TABLE" Then
  Response.Write "<strong>" & tbl.Name & "</strong><br />"
  Response.Write "Date Created: " & tbl.DateCreated & "<br />"
  sql = "SELECT Count(*) FROM " & tbl.Name 
  If Err.Number <> 0 Then REsponse.Write "!!!!!!!" & sql
  Set rs = Conn.Execute(sql) 
  Response.Write "Total Rows: " & rs(0) & "<br /><br /><strong>Columns</strong><br/>"
  rs.Close : Set rs = Nothing
  Set table = adox.Tables(tbl.Name) 
    For Each column in table.columns 
	On Error Resume Next
	  Response.Write column.name & " (" & columnTypes(column.type) & ") <br>"
	  If err.Number<>0 Then Response.Write err.Description
	  On Error Goto 0
    Next 
	If Not Left(tbl.Name,1) = "q" Then
      Response.Write "<br /><strong>" & tbl.Name & " Indexes</strong><br />"
	  For Each index In tbl.Indexes 
		colNames = "" 
		sortOrders = "" 
		For Each col In index.Columns 
		  colNames = colNames & col.Name 
		  so = "ASC" 
		  If col.SortOrder = 2 Then so = "DESC" 
			sortOrders = sortOrders & so  
		Next 
		
		Response.Write 	"Column Name(s): " &  colNames & _ 
		  "<br />Sort Order: " & sortOrders  
					
		Response.Write 	"<br/ >Index: " & index.Name & _ 
		  "<br />Unique: " & index.Unique & _ 
		  "<br />Clustered: " & index.Clustered & _ 
		  "<br />Primary Key: " & index.PrimaryKey  & "<br /><br />"
	  Next 
	End If	
	Response.Write "<br />"
  End If
Next

Response.Write "</td><td width=""25%"" valign=""top"">"
Dim proc
Response.Write "<h2>Procedures</h2>"

For Each proc in adox.Procedures	
  Response.Write "<strong>" & proc.Name & "</strong><br>"
  Response.Write "Date Created: " & proc.DateCreated & "<br />"
  Response.Write Replace(proc.Command.CommandText,chr(13),"<br />")
  Response.Write "<p>"

Next
Response.Write "</td><td width=""33%"" valign=""top"">"
Response.Write "<h2>Views</h2>"

For Each v in adox.Views
  Response.Write "<strong>" & v.Name & "</strong><br>"
  Response.Write "Date Created: " & v.DateCreated & "<br />"
  Response.Write Replace(v.Command.CommandText,chr(13),"<br />")
  Response.Write "<p>"
Next

Response.write "</td></tr></table>"

Set table = nothing 
Conn.close:  Set Conn = nothing 
Set adox = nothing 
%>

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 7415

0 Comments

Recent Comments

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...

Harris Boyce 04/07/2017 04:17
In response to Razor Pages - The Elevator Pitch
As a developer of a couple "trivial" web pages applications used by non-profits that wouldn't have I...

Cyrus 28/06/2017 20:25
In response to Razor Pages - Getting Started With The Preview
.net core 2.0 preview 2: <a...

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...