Results 1 to 4 of 4

Thread: Find a Table

  1. #1
    Join Date
    Aug 2004
    Posts
    36

    Unanswered: Find a Table

    Hello, everybody. I'm still converting a Paradox application into Access 2003 and I encountered a hopefully small problem. I have a button that imports a text file as a table. That's working fine. But in the Paradox app., I check to make sure the table name isn't already used. It's a simple:

    if istable(dbname) then
    msg(yadayada)
    return
    endif

    I can't seem to find a similar procedure in Access. I just want to see if the table already exists. And it's Friday afternoon and my brain hurts.

    TIA

  2. #2
    Join Date
    May 2005
    Posts
    1,191
    I found this code after a quick google, there was no name listed on the code to give credit to.
    Code:
    Function TableExists(strTableName As String) As Integer
    'Returns True if table exists
    
    Dim dbs As Database
    Set dbs = CurrentDb()
    
    Dim i As Integer
    TableExists = False
    dbs.TableDefs.Refresh
    For i = 0 To dbs.TableDefs.Count - 1
        If strTableName = dbs.TableDefs(i).Name Then
            'Table Exists
            TableExists = True
            Exit For
        End If
    Next i
    
    Set dbs = Nothing
    
    End Function
    Me.Geek = True

  3. #3
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    An alternative is:

    Code:
    Public Function TableExists(strTableName As String) As Boolean
    
        Dim strName As String
        
        On Error GoTo ErrorHandler
        
        TableExists = True
        
        strName = CurrentDb().TableDefs(strTableName).Name
        
        Exit Function
        
    ErrorHandler:
    
        TableExists = False
        
    End Function
    It checks the Name property for a given table which will generate and error if the table does not exist.

  4. #4
    Join Date
    Aug 2004
    Posts
    36
    Thanks. They both worked great.

Posting Permissions

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