General discussion

Locked

VB6 - Verifying if MS Access Table exists in database

By ashley.rowbottom ·
I am in vb and have an sql string created to drop/create/insert data into a table. Is there someway I could check to see if the tables exists before I try to drop it.

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

wrong forum

by john.a.wills In reply to VB6 - Verifying if MS Acc ...

This question belongs in Technical Q & A. I will look for it there, because I am interested in the subject.

Collapse -

Grr. code got wrapped and user error!

by steven_v_brown In reply to wrong forum

In 2nd example
Remove Prens around "cnn.execute("DROP TABLE [YOUR TABLE NAME]")"

In 3rd example code lines wrapped (Update ConnectionString property with your DB name)

Collapse -

There are many answers to this question - here's 3

by steven_v_brown In reply to VB6 - Verifying if MS Acc ...

1st: (Most likely the fastest)

Run a query of the SysObjects Table

SELECT MSysObjects.Name, MSysObjects.Type
FROM MSysObjects
WHERE (MSysObjects.Name="Your Table Name") AND (MSysObjects.Type=1)

2nd: (sneaky but works)

On error resume next
err.number = 0
'Asssume your ADO data connection is cnn...
cnn.execute("DROP TABLE [YOUR TABLE NAME]")
'Discard error...
err.number = 0
'Create your table...

3rd: (Snaziest)
'Walk the Jet table collection...


Reference: (Use Late Binding if you do not want to reference)
Microsoft ActiveX Data Objects 2.x Library (This is ADODB)
Microsoft ADO Ext. 2.8 for DDL and Security (This is ADOX)


Call the fuction: msgbox TableExists("ABC")

Past the following code:

Option Explicit
'----------------------------------------------------------------
Public Function TableExists(TableName As String) As Boolean
On Error GoTo ErrorHandle:
Dim cnn As ADODB.Connection
'Dim cnn
Dim cat As ADOX.Catalog
'Dim cat
Dim tbl As ADOX.Table
'Dim tbl
'--------------------------------------------------------------
'Connect to the Database...
'--------------------------------------------------------------
Set cnn = CreateObject("ADODB.Connection")
'cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YourDB.mdb;Persist Security Info=False"
cnn.Open
'--------------------------------------------------------------
'Set cat = New ADOX.Catalog
Set cat = CreateObject("ADOX.Catalog")
cat.ActiveConnection = cnn
'--------------------------------------------------------------
For Each tbl In cat.Tables
If tbl.Type = "TABLE" And tbl.Name = TableName Then
TableExists = True
End If
Next
'--------------------------------------------------------------
ExitProc:
On Error Resume Next
Set tbl = Nothing
Set cat = Nothing
cnn.Close
Set cnn = Nothing
Exit Function
ErrorHandle:
TableExists = False
GoTo ExitProc:
End Function
'----------------------------------------------------------------

Collapse -

There are many answers to this question - here's 3

by steven_v_brown In reply to VB6 - Verifying if MS Acc ...

1st: (Most likely the fastest)

Run a query of the SysObjects Table

SELECT MSysObjects.Name, MSysObjects.Type
FROM MSysObjects
WHERE (MSysObjects.Name="Your Table Name") AND (MSysObjects.Type=1)

2nd: (sneaky but works)

On error resume next
err.number = 0
'Asssume your ADO data connection is cnn...
cnn.execute("DROP TABLE [YOUR TABLE NAME]")
'Discard error...
err.number = 0
'Create your table...

3rd: (Snaziest)
'Walk the Jet table collection...


Reference: (Use Late Binding if you do not want to reference)
Microsoft ActiveX Data Objects 2.x Library (This is ADODB)
Microsoft ADO Ext. 2.8 for DDL and Security (This is ADOX)


Call the fuction: msgbox TableExists("ABC")

Past the following code:

Option Explicit
'----------------------------------------------------------------
Public Function TableExists(TableName As String) As Boolean
On Error GoTo ErrorHandle:
Dim cnn As ADODB.Connection
'Dim cnn
Dim cat As ADOX.Catalog
'Dim cat
Dim tbl As ADOX.Table
'Dim tbl
'--------------------------------------------------------------
'Connect to the Database...
'--------------------------------------------------------------
Set cnn = CreateObject("ADODB.Connection")
'cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YourDB.mdb;Persist Security Info=False"
cnn.Open
'--------------------------------------------------------------
'Set cat = New ADOX.Catalog
Set cat = CreateObject("ADOX.Catalog")
cat.ActiveConnection = cnn
'--------------------------------------------------------------
For Each tbl In cat.Tables
If tbl.Type = "TABLE" And tbl.Name = TableName Then
TableExists = True
End If
Next
'--------------------------------------------------------------
ExitProc:
On Error Resume Next
Set tbl = Nothing
Set cat = Nothing
cnn.Close
Set cnn = Nothing
Exit Function
ErrorHandle:
TableExists = False
GoTo ExitProc:
End Function
'----------------------------------------------------------------

Back to Web Development Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums