PDA

Click to See Complete Forum and Search --> : Checking to see if a table exists


Skeen
Nov 17th, 2000, 08:57 AM
How can I check if a table exist in a database using VB?

jp_schwartz
Nov 17th, 2000, 09:40 AM
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

honeybee
Nov 17th, 2000, 08:48 PM
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.