csiwa28
Programmer
- Apr 12, 2001
- 177
Is there a way to find out if a table exists in Access before you try to pull records?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Function GetTableName(ByVal db_name as String, tb_name as String)
Dim myFlg As Integer
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
' Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_name & ";"
conn.Open
' Use OpenSchema and get the table names.
myFlg = 0
Set rs = conn.OpenSchema(adSchemaTables, _
Array(Empty, Empty, Empty, "Table"))
Do While Not rs.EOF
If rs!TABLE_NAME = tb_name Then
myFlg = 1
Exit Do
Else
myFlg = 0
End If
rs.MoveNext
Loop
If myFlg = 1 Then
MsgBox "The table exists"
Else
MsgBox "The table does not exist"
End If
rs.Close
conn.Close
End Function