VB icon

A Better Way to Test For Tables (great for begginners - well commented)

Email
Submitted on: 1/7/2015 7:51:00 AM
By: Anonymous (from psc cd)  
Level: Beginner
User Rating: By 25 Users
Compatibility: VB 5.0, VB 6.0
Views: 1454
 
     This is the better way to find out whether your particular table, any table, exists in your database. Sequential is NOT the way. Check this out and let me know if you have any questions.

Please give me a vote if you like this code :)

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Better Way to Test For Tables (great for begginners - well commented)
' Description:This is the better way to find out whether your particular table, any table, exists in your database. Sequential is NOT the way. Check this out and let me know if you have any questions.
<P>
<P>Please give me a vote if you like this code :)
' By: Anonymous (from psc cd)
'**************************************

IF TableExists(strTableName) then MsgBox strTableName & " found." else MsgBox strTableName & " not found."
Private Function TableExists(TableName) As Boolean
'I ususally use a global Database object, however' you can just as easily pass it into the function if you'd prefer
Dim strTableName$ 'string
On Error GoTo NotFound
If TableName <> "" Then strTableName = dbMyDatabase.TableDefs(strTableName).Name
'If the table exists, the string will be filled, 'otherwise it will err out and TableExists will remain false.
TableExists = True
NotFound:
End Function
'I have VERY often seen people use the standard routine of
'going through EACH and EVERY table comparing each one till
'they get the the end, as in
 
 'For Each MyTable in DB.TableDefs
 ' if MyTable.Name = strNameImLookingFor then
 'TableExists = true
 'Exit For
 'end if
 'Next
'This is NOT the way to do this. You will unecesesarily use up
'yours as well as your users' very valuable time.
'Use this function. Make it private. When you pass the name
'of the table you need to check for into this routine, the
'recordset will either retrieve it, with a quickness, or it
'will error out, which is even quicker. If you have this in
'a private function, the erroring out will equate to it
'returning a negative response for the table search.
'I might add that this technique works superbly with field searches
'as well (such as Serial No, credit cards, socials, phone numbers, etc).
'And, there you have it.


Other 209 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.