Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to Check if table exits

Status
Not open for further replies.

iXPhound

Technical User
Feb 25, 2001
146
US
Hi all...beginner here so please excuse the simple questions. I have found examples on how to write a function that creates a table. Now i would like to create a function that checks if a table exits. So far I have:

Sub CheckTable(strTable as String)

Dim dbs as Database, tbl as TableDef
Set dbs = CurrentDB

LOL, can anyone help me finish...I am not finding my answer anywhere and I need some help. TIA!!!!
 
On Error Resume Next
Set tbl = dbs.TableDefs(strTable)
If Err Then
' table not found
Else
' table exists
End If
On Error GoTo 0

This is a little simplistic; it treats any error as "table not found".

You're a beginner? I'm surprised (but impressed) that you're using DAO already. I predict a bright future with Access! Rick Sprague
 
Function DoesTableExist(pTableName)
Dim db As Database
Dim tbl As TableDef
Set db = CurrentDb()
For Each tbl In db.TableDefs
If tbl.name = pTableName Then
DoesTableExist = True
End If
Next tbl
End Function

Sub MakeCall()
Debug.Print DoesTableExist("Table1")
End Sub

The function DoesTableExist take the parameter of the name of the table you want to check.

It then loops all the tables and returns true if it exists.
This code can be shortened.

But how it works is obvious.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top