How to check whether a table exiists (1 Viewer)

exaccess

Registered User.
Local time
Today, 08:20
Joined
Apr 21, 2013
Messages
287
I have an application with many forms and modules. My tables are stored in the backend and I connect them via links. No problem until the point when I upgraded the two ends to access 2007. Now my TAble exist function does not work it gives the error message "3376 table not found" and I cannot catch the message and go ahead. Help is appreciated. Here is the code:

Code:
Function TableExists(strTableName As String) As Boolean
    Dim db As Database
    Dim I As Integer
    On Error GoTo Err_Handler
    TableExists = False
    Set db = DBEngine.Workspaces(0).Databases(0)
    db.TableDefs.Refresh
    For I = 0 To db.TableDefs.count - 1
       If strTableName = db.TableDefs(I).Name Then
           TableExists = True
           Exit For
       End If
    Next I
TableExists_Exit:
    Set db = Nothing
    Exit Function
Err_Handler:
    Select Case Err.Number
    Case 3376
        TableExists = False
        Resume TableExists_Exit
    Case 3265
        TableExists = False
        Resume TableExists_Exit
    Case Else
        MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
        Resume TableExists_Exit
    End Select
End Function
 

jleach

Registered User.
Local time
Today, 02:20
Joined
Jan 4, 2012
Messages
308
Code:
Function TableExists(TableName As String) As Boolean
  On Error Resume Next
  Dim x As String
  x = CurrentDb.TableDefs(TableName).Name
  TableExists = Not CBool(Err.Number)
End Function
 

isladogs

MVP / VIP
Local time
Today, 07:20
Joined
Jan 14, 2017
Messages
18,209
Here's a variation on the same idea that doesn't depend on an error being triggered

Code:
Public Function CheckTableExists(strTable As String) As Boolean

'******************************
'CR 12/08/2008
'Checks if named table exists
'******************************

Dim strName As String
     
On Error Resume Next

'If table exists then strName will be <> ""
    strName = CurrentDb.TableDefs(strTable).Name
    CheckTableExists = Not (strName = "")
    
   'tidy up
    strTable = "" 
    strName = "" 
End Function
 

MarkK

bit cruncher
Local time
Yesterday, 23:20
Joined
Mar 17, 2004
Messages
8,180
Also worthy of note...
Code:
Function TableExists(Name As String) As Boolean
    TableExists = DCount( _
        "*", 
        "MSysObjects", _
        "Name = '" & Name & "' AND (Type = 1 Or Type = 6)")
End Function
Mark
 

isladogs

MVP / VIP
Local time
Today, 07:20
Joined
Jan 14, 2017
Messages
18,209
To include linked SQL Server tables, add Type = 4:

Code:
Function TableExists(Name As String) As Boolean
    TableExists = DCount( _
        "*", 
        "MSysObjects", _
        "Name = '" & Name & "' AND (Type = 1 Or Type = 4 Or Type = 6)")
End Function
 

Users who are viewing this thread

Top Bottom