UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Object Exists    
CODE
Function myObjExists(strObjNm As String, Optional intObjTyp As Integer = 2) As Boolean
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' Date contributed - Aug, 11, 2010
'
' You are free to use it in any application,
' provided this copyright notice is left unchanged.
'
' Tests if an object exists in the database
'
' intObjTyp can be:
   ' acTable (value 0)
   ' acQuery (value 1)
   ' acForm (value 2) Default
   ' acReport (value 3)
   ' acMacro (value 4)
   ' acModule (value 5)

Dim db As Database
Dim tbl As TableDef
Dim qry As QueryDef
Dim i As Integer, oTyp As String

   Set db = CurrentDb()
   myObjExists = False
   
   Select Case intObjTyp
      Case 0  ' Table
       For Each tbl In db.TableDefs
           If tbl.NAME = strObjNm Then
               myObjExists = True
               Exit Function
           End If
       Next tbl
      Case 1 ' Query
       For Each qry In db.QueryDefs
           If qry.NAME = strObjNm Then
               myObjExists = True
               Exit Function
           End If
       Next qry
    Case 2, 3 Or 5 ' Form or Report or Module
       If intObjTyp = 2 Then
           oTyp = "Forms"
         ElseIf intObjTyp = 3 Then
           oTyp = "Reports"
         Else
           oTyp = "Modules"
       End If
       For i = 0 To db.Containers(oTyp).Documents.Count - 1
           If db.Containers(oTyp).Documents(i).NAME = strObjNm Then
               myObjExists = True
               Exit Function
           End If
       Next i
    Case 4    ' Macro
       For i = 0 To db.Containers("Scripts").Documents.Count - 1
           If db.Containers("Scripts").Documents(i).NAME = strObjNm Then
               myObjExists = True
               Exit Function
           End If
       Next i
    Case Else
       MsgBox "Invalid Object Type passed, must be 0 (Table), 1 (Query), 2 (Form), 3 (Report), 4 (Macro), or 5 (Module)", vbInformation + vbOKOnly, "Incorrect Object Type"
   End Select
   
End Function


Creative Commons License
Object Exists by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 4,356 times.  This page was last modified 04:19, 12 September 2012 by Mark Davis. Contributions by Jack Leach  Disclaimers