Determine if internal database objects exist

Soon after ADO's release, Microsoft created an extension to the
object library called ActiveX Data Objects Extensions, or ADOX.
This library includes most of the DAO features missing from
standard ADO, including objects related to a database's schema.
With ADOX you can easily determine if a database contains a
specific table, view, or query.

To do so, set a reference to Microsoft ADO Ext. for DDL and
Security. For schema purposes, the ADOX consists of a Catalog
object, which contains the object collections that describe the
database, tables and views among them. To test for an existing
table in the Tables collection, you can either iterate through
the collection and check each item's name against the test
string; or you can use our favorite method, as seen in the
following code:

Private Sub Command1_Click()
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.3.51;" _
& "Data Source=C:Program FilesMicrosoft Visual StudioVB98Biblio.mdb"
On Error Resume Next
Set tbl = cat.Tables("MyTable")

If tbl Is Nothing Then
    MsgBox "MyTable doesn't exist"
    MsgBox "MyTable exists"
    Set tbl = Nothing
End If

Set cat = Nothing
Set con = Nothing
End Sub

You might also like...


ElementK Journals


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski