Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does table already exist?

Status
Not open for further replies.

WaltW

MIS
Jun 14, 2000
130
US
Is there a simple way in VBA to tell if a table you're about to create already exists? The only thing I know to do right now is look through all the table defs to see if the name is there. Is there a simpler, quicker way? Thanks.

WaltW
 
That is the recommended approach.



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Hello Walt;

My approach would be as follows:

Function DoesTableExist() As Boolean
Dim y As TableDef

On Error Resume Next
Set y = CurrentDb.TableDefs("table2")
If Err.Number = 0 Then DoesTableExist = True
End Function

The key here is the "On Error Resume Next". If the table (or open form or whatever) does not exist then an error occurs. By putting the checking process inside a function readability of your code is improved and reduces the chance that the "on error" will accidently foul up your other error handling routines. (The "On error resume next" applies only inside of the function.)

Bye;
Chell
 
Chell,
Generalize it?

Code:
Function DoesTableExist(MyTable as String) As Boolean
  Dim y As TableDef
  
  On Error Resume Next
  Set y = CurrentDb.TableDefs(MyTable)
  If Err.Number = 0 Then DoesTableExist = True
End Function

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top