General

6.185 Verificare se un determinato oggetto esiste in un deerminato database.
  Alessandro Baraldi

Per verificare se un determinato oggetto esiste in un determinato database, inserire il seguente codice VBA in un modulo standard del database e poi richiamare la funzione Esiste_Oggetto.
Public Const fForm = "Forms"
Public Const fReport = "Reports"
Public Const fMacro = "Scripts"
Public Const fModulo = "Modules"
Public Const fTabella = "Tables"
Public Const fQuery = "Queries"

Public Function Esiste_Oggetto(ByVal Nome_Ogg As String, _
                               Typ_Ogg As String, _
                   Optional ByVal Nome_Dbs As String = vbNullString) As
Boolean
'*****************************************************************
'Name      : Esiste_Oggetto (Function)
'Purpose   : Verifie if Database Object(Table, Query, Form or ...) Exist
'Author    : Alessandro Baraldi
'E.Mail    : ik2zok@libero.it
'Date      : 23 gennaio 2002
'Called by :
'Calls     :
'Inputs    : String=Object Name
'          : Type="Tables" or "Forms" or "Queries"
'          :      "Scripts" or "Reports" or "Modules"
'          : Nome_Dbs=Database.mdb (Source where Function search)
'Output    : True if Object Exist
'*****************************************************************
   Dim dbs As Database
   Dim tdf As TableDef
   Dim qdf As QueryDef
   Dim x, num_ogg As Integer
   If IsMissing(Nome_Dbs) Or Nome_Dbs = vbNullString Then
      Set dbs = CurrentDb
   Else
      Set dbs = OpenDatabase(Nome_Dbs)
   End If
   Select Case Typ_Ogg
      Case fTabella
         For Each tdf In dbs.TableDefs
            If tdf.Name = Nome_Ogg Then
               Esiste_Oggetto = True
               dbs.Close
               Set dbs = Nothing
               Exit Function
            End If
         Next tdf
      Case fQuery
         For Each qdf In dbs.QueryDefs
            If qdf.Name = Nome_Ogg Then
               Esiste_Oggetto = True
               dbs.Close
               Set dbs = Nothing
               Exit Function
            End If
         Next qdf
      Case fForm, fModulo, fMacro, fReport
        num_ogg = dbs.Containers(Typ_Ogg).Documents.Count
        For x = 0 To num_ogg - 1
           If dbs.Containers(Typ_Ogg).Documents(x).Name = Nome_Ogg Then
             Esiste_Oggetto = True
             dbs.Close
             Set dbs = Nothing
             Exit Function
           End If
        Next
   End Select
Esci:
   Esiste_Oggetto = False
   dbs.Close
   Set dbs = Nothing
End Function
La funzione Esiste_Oggetto() fa riferimento alla libreria Microsoft DAO quindi, se si usa una versione di Access successiva ad Access 97 aggiungere tale libreria ai riferimenti del database.


Se pensate di avere del materiale freeware interessante e volete pubblicarlo, allora leggete qui.