Check to see if table exists (1 Viewer)

Arry

Registered User.
Local time
Today, 09:38
Joined
Oct 24, 2003
Messages
31
In VBA i need to run code to check to see if a table exists. If it does i am appending records to the table from my VBA sql, but if it doesn't i need to create the table. I have created the code to append or create new but i cannot find out how to "check" to see if the table exists.

There must be a simple coding line to do this!!!

All help appricated.
 

Alex McDevitt

Registered User.
Local time
Today, 09:38
Joined
Nov 12, 2002
Messages
28
'******************** Code Start ************************
'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
Function fExistTable(strTableName As String) As Boolean
Dim db As Database
Dim i As Integer
Set db = DBEngine.Workspaces(0).Databases(0)
fExistTable = False
For i = 0 To db.TableDefs.Count - 1
If strTableName = db.TableDefs(i).Name Then
'Table Exists
fExistTable = True
Exit For
End If
Next i
Set db = Nothing
End Function

...The web is a wonderful thing....

Arry said:
In VBA i need to run code to check to see if a table exists. If it does i am appending records to the table from my VBA sql, but if it doesn't i need to create the table. I have created the code to append or create new but i cannot find out how to "check" to see if the table exists.

There must be a simple coding line to do this!!!

All help appricated.
 

namliam

The Mailman - AWF VIP
Local time
Today, 10:38
Joined
Aug 11, 2003
Messages
11,695
A 'oneliner':
Code:
Function DoesTableExist(yourTableName As String) As Boolean
    On Error Resume Next
    DoesTableExist = (yourTableName = CurrentDb.TableDefs(yourTableName).Name)
End Function
Using the function:
Code:
If DoesTableExist("TableName") then
    'append here
else
    'create new here
endif

Regards & GL
 

Users who are viewing this thread

Top Bottom