Check If Tables Exists

Lynn_AccessUser

Registered User.
Local time
Today, 10:27
Joined
Feb 4, 2003
Messages
125
I have code that creates 3 tables by linking to 3 .txt files. What I would like to do is check that all 3 tables were created.

This is what I have so far:

Dim db As Database
Dim tbl1 As TableDef
Dim tbl2 As TableDef
Dim tbl3 As TableDef

Set db = CurrentDb
Set tbl1 = db.CreateTableDef("table1")
Set tbl2 = db.CreateTableDef("table2")
Set tbl3 = db.CreateTableDef("table3")

For Each tbl1 In db.TableDefs
If tbl1.Name <> "table1" Or tbl2.Name <> "table2" Or tbl3.Name <> "table3" Then
MsgBox "Error Message"
Exit Sub
Else
MsgBox "All Files Were Linked"
Exit Sub
End If
Next
 
Send it the Table name and it will return True for tables that Exist and False for tables that dont exist.

Code:
Public Function DoesTableExist(stTableName As String) as Boolean
On Error Goto ErrorHandler
  Dim sTemp as String
  Dim db as Database
    db=Currentdb
    sTemp=db.TableDefs(stTableName).Name
    DoesTableExist=True

Exit Function
ErrorHandler:
  If Err.Number=3265 Then
    'Table Does Not Exist
    DoesTableExist=False
  Else
   msgbox Err.Number & vbcrlf & vbcrlf & Err.Description
  End If
End Function
 
I guess I am not following how to make your code work for more than one table, i.e., send the table name.
 
You will need to call the function for each table name you are searching for. This way it does not error out on the first one but lets you know for every table.
 

Users who are viewing this thread

Back
Top Bottom