The
Catalog
object is at the top of the ADOX object model. You can use this object to iterate through the
The initial procedure
SubCallMakeAJetDB() Dimstr1AsString 'Specifypathandfilenameforthenewdatabase str1="C:\Access11Files\Chapter03\MyNewDB.mdb" MakeAJetDBstr1 EndSub SubMakeAJetDB(str1AsString) OnErrorGoToMakeAJetDB_Trap Dimcat1AsADOX.Catalog 'Instantiatecatalog,andcreateanew 'databasefilebasedonit Setcat1=NewADOX.Catalog cat1.create"Provider=Microsoft.Jet.OLEDB.4.0;"&_ "DataSource="&str1 MakeAJetDB_Exit: 'Cleanupobjects Setcat1=Nothing ExitSub MakeAJetDB_Trap: IfErr.Number=-2147217897Then 'Whenthefilealreadyexists, 'killthepriorversion Debug.Printstr1 Kill(str1) Resume Else Debug.PrintErr.Number,Err.Description MsgBox"ViewImmediatewindowforerrordiagnostics.",_ vbInformation,"ProgrammingMicrosoftAccess2003" EndIf EndSub
The
Tables
collection in the ADOX object model offers an easy route for enumerating tables in an Access database. Table 3-1 shows that the
Tables
collection contains several types of tables. You might prefer to filter out one or more of these table types during a typical enumeration task. Besides the ADOX library, you can also use
AccessObject
objects to enumerate tables.
AccessObject
object types
The first code sample for enumerating tables lists all the tables within a catalog's
ActiveConnection
property setting. This setting is just a connection string that points at a data source through an ADO data provider. The sample consists of two procedures. The first procedure designates a data source argument for the connection string. The listing that appears here sets the string variable
str1
to the
SubCallListTablesRaw() Dimstr1AsString 'RunwitheitherbackupcopyofNorthwindorMyNewDB.mdb str1="C:\ProgramFiles\MicrosoftOffice\"&_ "Office11\Samples\Northwind.mdb" 'str1="C:\Access11Files\Chapter03\MyNewDB.mdb" ListTablesRawstr1 EndSub SubListTablesRaw(str1) Dimcat1AsADOX.Catalog Dimtbl1AsADOX.Table Dimstr2AsString Dimstr3AsString DimmwdAsInteger 'Instantiateacatalog,andpointittothetargetdatabase. Setcat1=NewADOX.Catalog cat1.ActiveConnection="Provider=Microsoft.Jet.OLEDB.4.0;"&_ "DataSource="&str1 'Computethelengthofthelongesttablenameinthedatabase. 'Assumeminimumlengthof5characters. mwd=5 ForEachtbl1Incat1.Tables IfLen(tbl1.Name)>mwdThenmwd=Len(tbl1.Name) Nexttbl1 mwd=mwd+1 'PrintarowtotheImmediatewindowcontainingthename 'andtypeofmemberinthecatalog'sTablescollection ForEachtbl1Incat1.Tables str2=tbl1.Name str3=String(mwd-Len(str2),"") Debug.Printstr2&str3&tbl1.Type Nexttbl1 'Cleanupobjects Setcat1=Nothing EndSub
After its declarations, the second procedure instantiates a
Catalog
object and sets its
ActiveConnection
property to the path and filename passed from the first procedure. Next, the second procedure
Figure 3.2:
Output from the
CallListTablesRaw
and
ListTablesRaw
procedures for a clean backup copy of the Northwind database.
The output of MyNewDB might surprise you. Running
CallListTablesRaw
on MyNewDB generates output even before we populate the database file with any
The preceding sample includes some of the resources that table enumeration can deliver to an application. However, instead of having a broad array of every table type, you can specify the return of a small subset of the total tables by designating the return of just one table type. By implementing this capability, you can gather precise information about the tables in any Access database file on a local area network (LAN) or wide area network (WAN). The reduced number of items associated with a filtered subset makes for a better record source for a combo box or list box. User interfaces work best when they show users only the information that they need to view.
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
The next pair of procedures implements filtering to return just one type of table. The first procedure has three
SubCallListTablesTypeFilter() Dimstr1AsString Dimstr2AsString Dimbol1AsBoolean str1="C:\ProgramFiles\MicrosoftOffice\"&_ "Office11\Samples\Northwind.mdb" str2="TABLE" bol1=False ListTablesTypeFilterstr1,str2,bol1 EndSub SubListTablesTypeFilter(str1AsString,_ str2AsString,bol1AsBoolean) Dimcat1AsADOX.Catalog Dimtbl1AsADOX.Table Dimstr3AsString Dimstr4AsString DimmwdAsInteger 'Instantiatecatalog,andconnecttoCurrentProject 'oranotherdatabase. Setcat1=NewADOX.Catalog Ifbol1=TrueThen Setcat1.ActiveConnection=_ CurrentProject.Connection Else cat1.ActiveConnection=_ "Provider=Microsoft.Jet.OLEDB.4.0;"&_ "DataSource="&str1 EndIf 'CreatereferencetoTablescollectionforcatalog, 'anddeterminelongesttablenameinthecatalog. mwd=5 ForEachtbl1Incat1.Tables If(tbl1.Type=str2)Then_ IfLen(tbl1.Name)>mwdThenmwd=Len(tbl1.Name) Nexttbl1 mwd=mwd+1 'Printtablenamesandtheirtype. ForEachtbl1Incat1.Tables Iftbl1.Type=str2Then str3=tbl1.Name str4=String(mwd-Len(str3),"") Debug.Printstr3&str4&tbl1.Type EndIf Nexttbl1 'Cleanupobjects. Setcat1=Nothing EndSub
The second procedure in this sample employs the design of the previous sample, with one major exception. This exception
The Details view in the Access Database window returns more than just the name of a table and its type. It also returns two other particularly useful bits of information, the table's date of creation and its last modification date. Because the Table object exposes DateCreated and DateModified properties, generating a display like the one in the Database window's Details view is relatively straightforward ”just make the width of the Description column zero after clicking the Details control on the Database window.
To keep the design of the next code listing simple, the first procedure passes just one argument to the second procedure. This argument is a string parameter that designates the filename and path for the target database file. The argument designates the Northwind_backup.mdb file. In my office, I use the Northwind_backup.mdb file when I want to work with an unedited version of the Northwind database. Recall that I initially referred to the Northwind_backup.mdb file in the "Connecting to Jet Databases" section of Chapter 1. The second procedure has hard-coded filters that return only user-defined tables in the target database. Because this sample generates four
SubCallTablesDetail()
Dimstr1AsString
str1="C:\ProgramFiles\MicrosoftOffice\"&_
"Office11\Samples\Northwind_backup.mdb"
TablesDetailstr1
EndSub
SubTablesDetail(str1AsString)
Dimcat1AsADOX.Catalog
Dimtbl1AsTable
DimmwdAsByte
'Instantiateacatalog,andpointitatthetargetdatabase.
Setcat1=NewADOX.Catalog
cat1.ActiveConnection="Provider=Microsoft.Jet.OLEDB.4.0;"&_
"DataSource="&str1
'Computethelengthofthelongesttablenameinthedatabase.
'Minimumlengthof10providesforthelengthofthecolumnheader.
mwd=10
ForEachtbl1Incat1.Tables
Iftbl1.Type="TABLE"Then_
IfLen(tbl1.Name)>mwdThenmwd=Len(tbl1.Name)
Nexttbl1
mwd=mwd+1
'PrintarowofcolumnheaderstotheImmediatewindowbefore
'printingthename,datemodified,datecreated,andtypeofmembers
'inthecatalog'sTablescollection.
Debug.Print"TableName"&String(mwd-Len("TableName"),"")&_
"DateModified"&String(22-Len("DateModified"),"")&_
"DateCreated"&String(22-Len("DateCreated"),"")&"Type"
ForEachtbl1Incat1.Tables
Iftbl1.Type="TA