Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a table exists in MS Access for vb macros [duplicate]

Tags:

vba

ms-access

Possible Duplicate:
Check if access table exists

I'm new to vba macros. Any idea how to check if a table exists or not? I have searched for previous posts but did not get a clear solution for this.

like image 831
Karthik Avatar asked Jul 28 '10 07:07

Karthik


4 Answers

Setting a reference to the Microsoft Access 12.0 Object Library allows us to test if a table exists using DCount.

Public Function ifTableExists(tblName As String) As Boolean

    If DCount("[Name]", "MSysObjects", "[Name] = '" & tblName & "'") = 1 Then

        ifTableExists = True

    End If

End Function
like image 143
Karthik Avatar answered Oct 22 '22 14:10

Karthik


I know the question is already answered, but I find that the existing answers are not valid:
they will return True for linked tables with a non working back-end.
Using DCount can be much slower, but is more reliable.

Function IsTable(sTblName As String) As Boolean
    'does table exists and work ?
    'note: finding the name in the TableDefs collection is not enough,
    '      since the backend might be invalid or missing

    On Error GoTo hell
    Dim x
    x = DCount("*", sTblName)
    IsTable = True
    Exit Function
hell:
    Debug.Print Now, sTblName, Err.Number, Err.Description
    IsTable = False

End Function
like image 33
iDevlop Avatar answered Oct 22 '22 13:10

iDevlop


Exists = IsObject(CurrentDb.TableDefs(tablename))
like image 12
Tobiasopdenbrouw Avatar answered Oct 22 '22 12:10

Tobiasopdenbrouw


Access has some sort of system tables You can read about it a little here you can fire the folowing query to see if it exists ( 1 = it exists, 0 = it doesnt ;))

SELECT Count([MSysObjects].[Name]) AS [Count]
FROM MSysObjects
WHERE (((MSysObjects.Name)="TblObject") AND ((MSysObjects.Type)=1));
like image 2
Sjuul Janssen Avatar answered Oct 22 '22 12:10

Sjuul Janssen