Modules

5.75 Verificare se nel database corrente esiste un determinato oggetto (2)
  Sandro Peruz

La funzione che segue serve a verificare se nel database corrente sia presente o meno un determinato oggetto.
Public Function MyObject(ByVal strname As String, ByVal ObjectType As Integer) As Boolean
    On Error GoTo MyObject_err
    MyObject = True
    Dim obj As AccessObject, dbs As Object
    Select Case ObjectType
        Case 0
            Set dbs = Application.CurrentData.AllTables(strname)
        Case 1
            Set dbs = Application.CurrentData.AllQueries(strname)
        Case 3
            Set dbs = Application.CurrentProject.AllMacros(strname)
        Case 4
            Set dbs = Application.CurrentProject.AllModules(strname)
        Case Else
            Set dbs = Application.CurrentProject.AllDataAccessPages(strname)
    End Select
MyObject_Exit:
    On Error GoTo 0
    Exit Function
MyObject_err:
    MyObject = False
    Resume MyObject_Exit
End Function
Se per esempio voglio verificare se nel database corrente esiste la tabella chiamata MyTable richiamerò la funzione MyObject nel seguente modo:
If MyObject("MyTable", 0) = True Then
     ' la maschera MiaForm esiste
Else
     ' la maschera MiaForm NON esiste
End If
Nel richiamo della funzione il primo argomento è una stringa che contiene il nome dell'oggetto.
Come secondo argomento invece si indicherà il valore 0 (zero) se l'oggetto cercato è una tabella, il valore 1 se è una query, il valore 3 se è una macro, il valore 4 se è un modulo ed il valore 2 per gli altri oggetti.

Nota
La funzione MyObject può essere usata solamente con versioni di Access successive ad Access 97.


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