Microsoft KB Archive/129927

= How To Determine If a Table or Query Exists =

Article ID: 129927

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q129927



SUMMARY
This article shows, by example, how to determine if a given table or query exists in a database opened by Visual Basic.



Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Add a module (Module1) to your Project. If using VB 5.0, establish a reference to Microsoft DAO 3.x Object Library using the References command from the Project menu.  Add the following line to the General Declarations section of Module1: Public Const NameNotInCollection = 3265   Add the following code to the General Declarations section of Form1: Dim DB As Database

Private Function ExistsTableQuery(TName As String) As Boolean Dim Test As String On Error Resume Next

' See if the name is in the Tables collection: Test = db.TableDefs(TName).Name If Err = 0 Then ExistsTableQuery = True Else ' Reset the error variable: Err = 0 ' See if the name is in the Queries collection: Test = db.QueryDefs(TName).Name If Err = 0 Then ExistsTableQuery = True End If        End If      End Function   Add the following code to the Form1_Load procedure: Private Sub Form_Load Set DB = DBEngine.Workspaces(0).Opendatabase("Biblio.mdb") Debug.Print "BadTable " ;IIF (ExistsTableQuery("BadTableName"), _        "does", "doesn't"); " exist." Debug.Print "Authors " ;IIF (ExistsTableQuery("Authors"), _        "does", "doesn't"); " exist." End Sub   Start the program by choosing Start from the Run menu or by pressing the F5 key. The Immediate window correctly prints: BadTable doesn't exist. Authors does exist. </li></ol>

Additional query words: kbHowTo kbVBp400 kbVBp500 kbVBp600 kbdse kbDSupport kbVBp

Keywords: kbhowto KB129927

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.