Skip to main content

Function to Check If a Table Exists in Access Database

We can create a function to check whether a specific table exist in MS Access Database. There are at least two functions we can create to evaluate the existence of a table in Access, one for external database and the other for current database. Let's name isTableExist function for checking the existence of a table in external database and isTableExistCurrentDbs function for checking the existence of a table in current database.

Function to check if a table exist in Access external database

Function isTableExist(strTableName As String) As Boolean
  Dim tbl As DAO.TableDef
On Error GoTo Err_Msg
  
  isTableExist = False
  For Each tbl In daoDbs.TableDefs
    If tbl.Name = strTableName Then
      isTableExist = True
      Exit Function
    End If
  Next tbl
Exit_Function:
  Exit Function
Err_Msg:
  MsgBox "Function isTableExist, Error # " & str(Err.Number) & ", source: " & Err.Source & _
  Chr(13) & Err.description
  Resume Exit_Function
End Function

 

Implementation


To execute isTableExist function, at first, we must open a database. It is performing letDbsOpen function. After opening a database, we determine the table to be searched for. For example, there is a database that has been predetermined in the letDbsOpen function. We may run the function in Immediate Window as follows:
?letDbsOpen
Next, we determine the table name, say for example, a table named tblVoucherTemp as seen in the above figure. To check the existence of tblVoucherTemp, simply type the following code in Immediate Window:
?isTableExist("tblVoucherTemp")
True
The output is true
?isTableExist("tblVoucherTmp")
False
The above code produce False result, because no table named tblVoucherTmp exists.

 

Function to check if a table exist in Access current database

Function isTableExistCurrentDbs(strTableName As String) As Boolean
  Dim objTbl As Object
On Error GoTo Err_Msg
  
  isTableExistCurrentDbs = False
    
  For Each objTbl In Application.CurrentData.AllTables
    If objTbl.Name = strTableName Then
      isTableExistCurrentDbs = True
      Exit Function
    End If
  Next objTbl
Exit_Function:
  Exit Function
Err_Msg:
  MsgBox "Function isTableExistCurrentDbs, Error # " & str(Err.Number) & ", source: " & Err.Source & _
  Chr(13) & Err.description
  Resume Exit_Function
End Function

 

Implementation


To execute isTableExistCurrentDbs function, we do not have to perform letDbsOpen function. Using the current database, we determine the table to be searched for. For example, there is a table named tblTempVoucherTemp in the current database.

To check the existence of tblTempVoucherTemp, simply type the following code in Immediate Window:
?isTableExistCurrentDbs("tblTempVoucherTemp")
True
The output is true
?isTableExistCurrentDbs("tblTempVoucherTmp")
False
The above code produce False result, because no table named tblTempVoucherTmp exists.

Comments

Popular posts from this blog

Get Connection String to Access DAO Database

To specify the connection string to a database, we use getDbsConString function. There are, at least, two variables to be set to specify the connection string. They are strProvider and strPassword. The strProvider variable specifies database provider to be employed, for example: the provider for MS Access database is MS Access. If the connection string has a password, then the strPassword variable must also be set. The result of getDbsConString function is a string value.

Creating Master Table for Subaccounts

In our accounting database, we have two subaccounts that are part of the primary accounts. They are some sort of derivative accounts for the primary accounts. The two subaccounts may serve as profit center, cost center, organization unit, vendor, customer, tax, etc. The subaccounts are stored in two tables, one for subaccounts 1 and the other for subaccounts 2. Following are the explanation for both two tables.

Function to Get Data Type DAO

Data type is the most important property in a field. Data type determines what type of data should be stored in a field of a table and how to present the data in a report. For example, a field with Text data type stores data in a text or numerical character. For a field that has Numeric data type, this field can only stores numerical data.