Results 1 to 3 of 3

Thread: Checking to see if a table exists

  1. #1

    Thread Starter
    Addicted Member Skeen's Avatar
    Join Date
    Jul 2000
    Location
    Abingdon, Oxon
    Posts
    138

    Wink

    How can I check if a table exist in a database using VB?
    "It wasn't the booze that made me snooze, It was the Gin that did me in!"

  2. #2
    Hyperactive Member
    Join Date
    Nov 2000
    Location
    Mexico City
    Posts
    306

    Cool

    try this:
    Note: I declared here the database, but you have to use your main connection (of your app).

    Function f_ExistYourTable(str_Table As String) As Boolean

    Dim int_I As Integer
    Dim db As Database

    f_ExistYourTable = False
    For int_I = 0 To db.TableDefs.Count - 1
    If UCase$(Trim$(str_Table)) = UCase$(Trim$(db.TableDefs(int_I).Name)) Then
    f_ExistYourTable = True
    Exit Function
    End If
    Next

    End Function
    If things were easy, users might be programmers.

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    There are a number of ways to do this:

    You can follow JP's way with Access. With other databases, it may not work. What you can use is :

    System tables: In Oracle, SQL Server or even in Access you have system tables in the database to store information about all objects in the database. Use these tables to find out if your table exists in them.

    Error Trapping: You can trap almost all database errors in VB. So just put the error handler where you are accessing a table. If the table does not exist, you can trap the specific error and take necessary action.

    You will try to discard the second approach, but it has a big advantage over others. You don't have to check for the existence of a table every time you use it in a query.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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