Check for table name

  • Thread starter Thread starter Frederick Wilson
  • Start date Start date
F

Frederick Wilson

Is there a way to check to see if a table of a particular name?

Currently I am looping through each table in the db to check each name.
As the db grows this process takes longer.

Thanks,
--
Frederick Wilson

_____________________________________
for multimedia design services visit
http://www.legalanimatics.com
 
You could simply try to look for the tablename, and trap the error that
occurs if it doesn't exist:

Function TableExists(TableName As String) As Boolean
On Error Resume Next

Dim strName As String

strName = CurrentDb.TableDefs(TableName).Name
TableExists = (Err.Number = 0)

End Function
 
Douglas said:
You could simply try to look for the tablename, and trap the error that
occurs if it doesn't exist:

Function TableExists(TableName As String) As Boolean
On Error Resume Next

Dim strName As String

strName = CurrentDb.TableDefs(TableName).Name
TableExists = (Err.Number = 0)

End Function
Very interesting concept.

I am not sure I understand TableExists = (Err.Number = 0) how can you
set the err.number? Or is this what is happening?

Sorry, these might seem like dumb question but I have never really coded
to expect an error and then use this fact.

Thanks,

--
Frederick Wilson

_____________________________________
for multimedia design services visit
http://www.legalanimatics.com
 
Frederick Wilson said:
Very interesting concept.

I am not sure I understand TableExists = (Err.Number = 0) how can you set
the err.number? Or is this what is happening?

Sorry, these might seem like dumb question but I have never really coded
to expect an error and then use this fact.

What's in the parentheses is a boolean comparison. If Err.Number equals 0,
it will evaluate to True. If Err.Number doesn't equal 0, it will evaluate to
False. TableExists, therefore, is getting set to either True or False.

The "normal" value for Err.Number is 0: that means there is no error. So
what the code does is attempt to access the Name property of the TableDef
object. If it can, Err.Number should be 0, so the function will return True.
If it can't, Err.Number will be something other than 0, so the function will
return False. There's no magic to why I chose the Name property: I could
have chosen any other property.
 
Douglas said:
What's in the parentheses is a boolean comparison. If Err.Number equals 0,
it will evaluate to True. If Err.Number doesn't equal 0, it will evaluate to
False. TableExists, therefore, is getting set to either True or False.

The "normal" value for Err.Number is 0: that means there is no error. So
what the code does is attempt to access the Name property of the TableDef
object. If it can, Err.Number should be 0, so the function will return True.
If it can't, Err.Number will be something other than 0, so the function will
return False. There's no magic to why I chose the Name property: I could
have chosen any other property.
Doug,

Thanks for taking the time to explain this to me. I've never heard or
figured out that the parentheses is a boolean comparison. This could
change a lot of things I do.

Fred


--
Frederick Wilson

_____________________________________
for multimedia design services visit
http://www.legalanimatics.com
 
Frederick Wilson said:
Douglas J. Steele wrote:

Thanks for taking the time to explain this to me. I've never heard or
figured out that the parentheses is a boolean comparison. This could
change a lot of things I do.

Just a slight correction. It's not the parentheses that make it a boolean
comparison: it's the equal sign.

I added the parentheses in an attempt to make it easier to understand
(obviously I failed <g>), and to ensure that the correct comparison was
made.

Some other languages use different operators for assignment and comparison
(for example, C and C++ use = for assignment and == for comparison). If VB
and VBA did that, perhaps it would have been more obvious what I was doing.
 
Douglas said:
Just a slight correction. It's not the parentheses that make it a boolean
comparison: it's the equal sign.

I added the parentheses in an attempt to make it easier to understand
(obviously I failed <g>), and to ensure that the correct comparison was
made.

Some other languages use different operators for assignment and comparison
(for example, C and C++ use = for assignment and == for comparison). If VB
and VBA did that, perhaps it would have been more obvious what I was doing.
Doug,

At any rate, this suggestion work GREAT! The speed increase in execution
was amazing. I did not do a full blown regression test but in the
current operation it is wonderful. Thanks a bunch!

Fred

--
Frederick Wilson

_____________________________________
for multimedia design services visit
http://www.legalanimatics.com
 
Back
Top