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 
%>

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

0 Comments

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....