Results 1 to 6 of 6

Thread: [RESOLVED] How to test if table exists

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Resolved [RESOLVED] How to test if table exists

    Access, VB

    There must be a simpler way to test if a table exists in Access rather than having to loop arround all the objects ... isn't there? My code is:
    VB Code:
    1. Dim catLocal As ADOX.Catalog
    2. Dim tblLinked As ADOX.Table
    3. Dim lintTable As Integer
    4. Dim lblnTable as boolean
    5.  
    6. ' Create a connection to QuoteWerks
    7. Set catLocal = New ADOX.Catalog
    8. catLocal.ActiveConnection = lcnnQW
    9.  
    10. ' Loop arround all the objects
    11. For lintTable = 0 To catLocal.Tables.Count - 1
    12.     If CStr(catLocal.Tables.Item(lintTable).Name) = lstrQWTable Then
    13.         lblnTable = True
    14.         Exit For
    15.     End If
    16. Next ' Table
    (PS I know I posted this as part of another post but did not get a reply ... see How to add ODBC linked table in an Access database where I still have a problem with creating the primary key to a linked table)
    Last edited by chilling; Sep 16th, 2005 at 11:52 AM.
    chilling

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: How to test if table exists

    Here is a function I have used with MySQL. I would assume the errors would be the same :

    VB Code:
    1. Function TableExists(sName As String) As Boolean
    2.     On Error GoTo ErrHandler
    3.    
    4.     TableExists = True
    5.    
    6.     Rs.CursorType = adOpenStatic
    7.     Rs.CursorLocation = adUseClient
    8.     Rs.LockType = adLockPessimistic
    9.     Rs.Source = "Select * From " & sName & " Where 1=2"
    10.     Set Rs.ActiveConnection = Mysql_Connection
    11.     Rs.Open
    12.     Rs.Close
    13.    
    14.     Exit Function
    15.    
    16. ErrHandler: If Err.Number = -2147217865 Then
    17.                 TableExists = False
    18.                 Err.Clear
    19.             ElseIf Err.Number = -2147217900 Then
    20.                 TableExists = True
    21.                 Err.Clear
    22.             Else
    23.                 MsgBox Err.Number & " - " & Err.Description
    24.                 Err.Clear
    25.             End If
    26. End Function


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: How to test if table exists

    You can check if there exists a record in table MSysObjects (it's a hidden system table) where the Name column is your table name and column Type=4 (tables).

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to test if table exists

    I think manavo11's way of running a query against the table is probably the easiest, most straightforward way of checking. If it doesn't exist and error will be returned which has been trapped for and bada bing bada boom, you know immediately if it is there.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: How to test if table exists

    Thanks guys.... for that. Any thoughts on my problem for not being able to create a Primary Key index when creating a linked table in access: See How to add ODBC linked table in an Access database
    chilling

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to test if table exists

    Well, I don't know how to do it using ADOX, but this is how I do it using ADO. This, by the way, is pulled directly out of one of my projects that is currently in production.
    Code:
    gsSql = "CREATE TABLE CUSTOM_SETTINGS ("
    gsSql = gsSql & "  ROW_ID INT PRIMARY KEY,"
    gsSql = gsSql & "  KEY_SECTION VARCHAR(65) NULL,"
    gsSql = gsSql & "  KEY_NAME VARCHAR(65) NULL,"
    gsSql = gsSql & "  STRING_SETTINGS VARCHAR(256) NULL,"
    gsSql = gsSql & "  BOOL_SETTINGS INT NULL,"
    gsSql = gsSql & "  LONG_SETTINGS INT NULL"
    gsSql = gsSql & "   )"
    gdB.Execute gsSql

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width