Provided by Allen Browne, March 2007. Updated February 2008.
ADOX Programming Code Examples
This page is a reference for developers, demonstrating how to use the ADOX library to programmatically create, delete, modify, and list the objects in Access - the tables, fields, indexes, and relations, queries, and databases - and read or set their properties.
ADOX is an extension to the ADO library, exposing the catalog of database objects. To use this library, open the code window, choose References on the Tools menu, and check the box beside:
Microsoft ADO Ext x.x for DDL and Security
In general, the DAO library is better than ADOX. DAO is purpose-designed for Access, and exposes properties the other libraries don't. But there are some things DAO cannot do, such as setting the Seed of an AutoNumber field. The ADOX library is less stable, and more subject to version problems, so if you strike problems with the code in this page, an MDAC update might address the issue for you.
See the field type reference for a comparison of the field types in ADOX compared to the Access interface and other libraries.
There is no explanation of the code beyond in-line comments, and no error handling in most examples.
Option Compare Database
Option Explicit
Function SetSeed(strTable As String, strAutoNum As String, lngID As Long) As Boolean
Dim cat As New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
cat.Tables(strTable).Columns(strAutoNum).Properties("Seed") = lngID
Set cat = Nothing
SetSeed = True
End Function
Function ShowAllTables(Optional bShowFieldsToo As Boolean)
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Set cat.ActiveConnection = CurrentProject.Connection
For Each tbl In cat.Tables
Debug.Print tbl.Name, tbl.Type
If bShowFieldsToo Then
For Each col In tbl.Columns
Debug.Print , col.Name, col.Type
Next
Debug.Print "--------------------------------"
End If
Next
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function
Function ShowPropsADOX(strTable As String, Optional bShowPropertiesToo As Boolean)
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Dim prp As ADOX.Property
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables(strTable)
For Each col In tbl.Columns
Debug.Print col.Name
If bShowPropertiesToo Then
For Each prp In col.Properties
Debug.Print , prp.Name, prp.Type, prp.Value
Next
Debug.Print "--------------------------------"
End If
Next
Set prp = Nothing
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function
Function CreateTableAdox()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = New ADOX.Table
tbl.Name = "tblAdoxContractor"
With tbl.Columns
.Append "ContractorID", adInteger
.Append "Surname", adVarWChar, 30
.Append "FirstName", adVarWChar, 20
.Append "Inactive", adBoolean
.Append "HourlyFee", adCurrency
.Append "PenaltyRate", adDouble
.Append "BirthDate", adDate
.Append "Notes", adLongVarWChar
.Append "Web", adLongVarWChar
With !ContractorID
Set .ParentCatalog = cat
.Properties("Autoincrement") = True
.Properties("Description") = "Automatically " & _
"generated unique identifier for this record."
End With
With !Surname
Set .ParentCatalog = cat
.Properties("Nullable") = False
.Properties("Jet OLEDB:Allow Zero Length") = False
End With
With !BirthDate
Set .ParentCatalog = cat
.Properties("Jet OLEDB:Column Validation Rule") = _
"Is Null Or <=Date()"
.Properties("Jet OLEDB:Column Validation Text") = _
"Birth date cannot be future."
End With
With !Web
Set .ParentCatalog = cat
.Properties("Jet OLEDB:Hyperlink") = True
End With
End With
cat.Tables.Append tbl
Debug.Print "tblAdoxContractor created."
Set tbl = Nothing
Set tbl = New ADOX.Table
tbl.Name = "tblAdoxBooking"
With tbl.Columns
.Append "BookingID", adInteger
.Append "BookingDate", adDate
.Append "ContractorID", adInteger
.Append "BookingFee", adCurrency
.Append "BookingNote", adWChar, 255
With !BookingID
.ParentCatalog = cat
.Properties("Autoincrement") = True
End With
With !BookingNote
.ParentCatalog = cat
.Properties("Nullable") = False
.Properties("Jet OLEDB:Allow Zero Length") = False
End With
End With
cat.Tables.Append tbl
Debug.Print "tblAdoxBooking created."
Set tbl = Nothing
Set cat = Nothing
End Function
Function ModifyTableAdox()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As New ADOX.Column
cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables("tblAdoxContractor")
With col
.Name = "MyDecimal"
.Type = adNumeric
.Precision = 28
.NumericScale = 8
End With
tbl.Columns.Append col
Set col = Nothing
Debug.Print "Column added."
tbl.Columns.Delete "MyDecimal"
Debug.Print "Column deleted."
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function
Function ModifyFieldPropAdox()
Dim cat As New ADOX.Catalog
Dim col As ADOX.Column
Dim prp As ADOX.Property
cat.ActiveConnection = CurrentProject.Connection
Set col = cat.Tables("MyTable").Columns("MyField")
Set prp = col.Properties("Nullable")
Debug.Print prp.Name, prp.Value, (prp.Type = adBoolean)
prp.Value = Not prp.Value
Set prp = Nothing
Set col = Nothing
Set cat = Nothing
End Function
Function DeleteTableAdox()
Dim cat As New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
cat.Tables.Delete "MyTable"
Set cat = Nothing
End Function
Function CreateIndexesAdox()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim ind As ADOX.Index
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables("tblAdoxContractor")
Set ind = New ADOX.Index
ind.Name = "PrimaryKey"
ind.PrimaryKey = True
ind.Columns.Append "ContractorID"
tbl.Indexes.Append ind
Set ind = Nothing
Set ind = New ADOX.Index
ind.Name = "Inactive"
ind.Columns.Append "Inactive"
tbl.Indexes.Append ind
Set ind = Nothing
Set ind = New ADOX.Index
ind.Name = "FullName"
With ind.Columns
.Append "Surname"
.Append "FirstName"
End With
tbl.Indexes.Append ind
Set ind = Nothing
Set tbl = Nothing
Set cat = Nothing
Debug.Print "tblAdoxContractor indexes created."
End Function
Function DeleteIndexAdox()
Dim cat As New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
cat.Tables("tblAdoxContractor").Indexes.Delete "Inactive"
Set cat = Nothing
End Function
Function CreateKeyAdox()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim ky As New ADOX.Key
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables("tblAdoxBooking")
With ky
.Type = adKeyForeign
.Name = "tblAdoxContractortblAdoxBooking"
.RelatedTable = "tblAdoxContractor"
.Columns.Append "ContractorID"
.Columns("ContractorID").RelatedColumn = "ContractorID"
.DeleteRule = adRISetNull
End With
tbl.Keys.Append ky
Set ky = Nothing
Set tbl = Nothing
Set cat = Nothing
Debug.Print "Key created."
End Function
Function ShowKeyAdox(strTableName As String)
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim ky As ADOX.Key
Dim strRIName As String
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables(strTableName)
For Each ky In tbl.Keys
With ky
Select Case .DeleteRule
Case adRINone
strRIName = "No delete rule"
Case adRICascade
strRIName = "Cascade delete"
Case adRISetNull
strRIName = "Cascade to null"
Case adRISetDefault
strRIName = "Cascade to default"
Case Else
strRIName = "DeleteRule of " & .DeleteRule & " unknown."
End Select
Debug.Print "Key: " & .Name & ", to table: " & .RelatedTable & ", with: " & strRIName
End With
Next
Set ky = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function
Function DeleteKeyAdox()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Set cat.ActiveConnection = CurrentProject.Connection
cat.Tables("tblAdoxBooking").Keys.Delete "tblAdoxContractortblAdoxBooking"
Set cat = Nothing
Debug.Print "Key deleted."
End Function
Function CreateViewAdox()
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command
Dim strSql As String
cat.ActiveConnection = CurrentProject.Connection
strSql = "SELECT BookingID, BookingDate FROM tblDaoBooking;"
cmd.CommandText = strSql
cat.Views.Append "qryAdoxBooking", cmd
Set cmd = Nothing
Set cat = Nothing
Debug.Print "View created."
End Function
Function CreateProcedureAdox()
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command
Dim strSql As String
cat.ActiveConnection = CurrentProject.Connection
strSql = "PARAMETERS StartDate DateTime, EndDate DateTime; " & _
"DELETE FROM tblAdoxBooking " & _
"WHERE BookingDate Between StartDate And EndDate;"
cmd.CommandText = strSql
cat.Procedures.Append "qryAdoxDeleteBooking", cmd
Set cmd = Nothing
Set cat = Nothing
Debug.Print "Procedure created."
End Function
Function ShowProx()
Dim cat As New ADOX.Catalog
Dim proc As ADOX.Procedure
Dim vw As ADOX.View
cat.ActiveConnection = CurrentProject.Connection
Debug.Print "Procedures: " & cat.Procedures.Count
For Each proc In cat.Procedures
Debug.Print proc.Name
Next
Debug.Print cat.Procedures.Count & " procedure(s)"
Debug.Print
Debug.Print "Views " & cat.Views.Count
For Each vw In cat.Views
Debug.Print vw.Name
Next
Set cat = Nothing
End Function
Function ExecuteProcedureAdox()
Dim cat As New ADOX.Catalog
Dim cmd As ADODB.Command
Dim lngCount As Long
cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Procedures("qryAdoxDeleteBooking").Command
cmd.Parameters("StartDate") = #1/1/2004#
cmd.Parameters("EndDate") = #12/31/2004#
cmd.Execute lngCount
Debug.Print lngCount & " record(s) deleted."
Set cmd = Nothing
Set cat = Nothing
End Function
Function DeleteProcedureAdox()
Dim cat As New ADOX.Catalog
Dim cmd As ADODB.Command
Dim lngCount As Long
cat.ActiveConnection = CurrentProject.Connection
cat.Procedures.Delete "qryAdoxDeleteBooking"
Set cat = Nothing
End Function
Function CreateDatabaseAdox()
Dim cat As New ADOX.Catalog
Dim strFile As String
strFile = "C:\SampleADOX.mdb"
cat.Create "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source='" & strFile & "'"
Set cat = Nothing
Debug.Print strFile & " created."
End Function
Function DeleteAllAndResetAutoNum(strTable As String) As Boolean
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Dim strSql As String
strSql = "DELETE FROM [" & strTable & "];"
CurrentProject.Connection.Execute strSql
cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables(strTable)
For Each col In tbl.Columns
If col.Properties("Autoincrement") Then
col.Properties("Seed") = 1
DeleteAllAndResetAutoNum = True
End If
Next
End Function
Function GetSeedADOX(strTable As String, Optional ByRef strCol As String) As Long
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Set cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat.Tables(strTable)
For Each col In tbl.Columns
If col.Properties("Autoincrement") Then
strCol = col.Name
GetSeedADOX = col.Properties("Seed")
Exit For
End If
Next
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function
Function ResetSeed(strTable As String) As String
Dim strAutoNum As String
Dim lngSeed As Long
Dim lngNext As Long
Dim strSql As String
Dim strResult As String
lngSeed = GetSeedADOX(strTable, strAutoNum)
If strAutoNum = vbNullString Then
strResult = "AutoNumber not found."
Else
lngNext = Nz(DMax(strAutoNum, strTable), 0) + 1
If lngSeed = lngNext Then
strResult = strAutoNum & " already correctly set to " & lngSeed & "."
Else
Debug.Print lngNext, lngSeed
strSql = "ALTER TABLE [" & strTable & "] ALTER COLUMN [" & strAutoNum & "] COUNTER(" & lngNext & ", 1);"
Debug.Print strSql
CurrentProject.Connection.Execute strSql
strResult = strAutoNum & " reset from " & lngSeed & " to " & lngNext
End If
End If
ResetSeed = strResult
End Function