Consulting

Results 1 to 4 of 4

Thread: Does an Object exist?

  1. #1
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    204
    Location

    Does an Object exist?

    How do I determine if an object (Form, table, Query...) exists before I make one? I want VBA to determine this. I suppose something like
    If Exists("Querya")......which, of course, is too direct and straightforward!

  2. #2
    VBAX Regular ___'s Avatar
    Joined
    Jun 2004
    Posts
    22
    Location
    Does this help?
    [vba]
    Option Compare Database
    Option Explicit

    Function ObjectExists(strObjectType As String, strObjectName As String) As Boolean
    Dim db As Database
    Dim tbl As TableDef
    Dim qry As QueryDef
    Dim i As Integer

    Set db = CurrentDb()
    ObjectExists = False

    Select Case strObjectType

    Case "Table"
    For Each tbl In db.TableDefs
    If tbl.Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next tbl

    Case "Query"
    For Each qry In db.QueryDefs
    If qry.Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next qry

    Case "Form", "Report", "Module"
    For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
    If db.Containers(strObjectType & "s").Documents(i).Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next i

    Case "Macro"
    For i = 0 To db.Containers("Scripts").Documents.Count - 1
    If db.Containers("Scripts").Documents(i).Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next i

    Case Else
    MsgBox "Must be a Table, Query, Form, Report, Macro, or Module"
    End Select

    End Function
    [/vba]

    HTH
    Nemo hic adest illius nominis
    ??????????????????
    ??????

  3. #3
    VBAX Tutor SJ McAbney's Avatar
    Joined
    May 2004
    Location
    Glasgow
    Posts
    243
    Location
    Personally, I'd enumerate the second parameter and ensure the DAO was explicitly reference. Still ___'s code though.

    [vba]
    Option Compare Database
    Option Explicit

    Enum otObjectTypes
    otTable
    otQuery
    otForm
    otReport
    otMacro
    otModule
    End Enum

    Function ObjectExists(strObjectType As String, bytObject As Byte) As Boolean
    Dim db As DAO.Database
    Dim tbl As DAO.TableDef
    Dim qry As DAO.QueryDef
    Dim i As Integer

    Set db = CurrentDb()
    ObjectExists = False

    Select Case bytObject

    Case otTable
    For Each tbl In db.TableDefs
    If tbl.Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next tbl

    Case otQuery
    For Each qry In db.QueryDefs
    If qry.Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next qry

    Case otForm, otReport, otModule
    For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
    If db.Containers(strObjectType & "s").Documents(i).Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next i

    Case otMacro
    For i = 0 To db.Containers("Scripts").Documents.Count - 1
    If db.Containers("Scripts").Documents(i).Name = strObjectName Then
    ObjectExists = True
    Exit For
    End If
    Next i

    Case Else
    MsgBox "Must be a Table, Query, Form, Report, Macro, or Module"
    End Select

    End Function [/vba]

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    That code won't compile.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •