Source code for Issue Number 17

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module (this is the desired way) or form. To do this, open up your project and insert a new Module. Change the name of the module to basDatabase and paste this code into the module.

'----------------------------------------------------------------------
'
'   Module Name:    basDatabase
'   Written By:     C&D Programming Corp.
'   Create Date:    12/26/97
'   Copyright:      Copyright 1997-98 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'   Purpose:        Check for existence of a table or query in a
'                   database.
'
'----------------------------------------------------------------------
Const dberr_NameNotInCollection = 3265

Public Function TableOrQueryExists(sDBName As String, sTableName As String) As Boolean
   Dim sTest As String
   Dim DB As Database
   On Error Resume Next

   ' Assume the table/query does not exist
   TableOrQueryExists = False

   ' Open Database, non-exclusively, Read-Only
   Set DB = OpenDatabase(sDBName, False, True)

   ' Check to see if the name of the table you are looking
   ' for is in the TableDefs collection of the database
   sTest = DB.TableDefs(sTableName).Name
   If Err <> dberr_NameNotInCollection Then
      TableOrQueryExists = True
      Exit Function

   Else

      ' Reset the error variable, just in case
      Err = 0

      ' Check to see if the name of the table you are looking
      ' for is in the QueryDefs collection in the database
      sTest = DB.QueryDefs(sTableName).Name
      If Err <> dberr_NameNotInCollection Then
         TableOrQueryExists = True
      End If

   End If
End Function