Share via

MSAccess VBA - Looping all objects in MSAccess Database

Anonymous
2023-04-28T11:16:25+00:00

Dear All,

from one MSAccess database I would like to remove all the Tables, Queries, Forms, Macros, Reports I have in an MSAccess database placed in a different location.

I know by a for each loop I can remove Tables and Queries but I need the same loop for Forms, Macros, Reports.

Any way loop through the all objects (Tables, Queries, Forms, Macros, Reports) in an MSAccess database?

I found this code:

Private Sub cmdOK_Click() 

    Dim Obj As AccessObject 

    Dim Dbs As Object 

    Set Dbs = OpenDatabase("D:\Folder\Test.accdb", False, False, ";pwd=" & DatabasePassword & "") 

    For Each Obj In Dbs.AllQueries 

        Debug.Print Obj.Name 

    Next Obj 

End Sub

but I get this error on this line For Each Obj In Dbs.AllQueries

Any suggestion?

Thanks

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2023-04-28T13:02:12+00:00

    Dbs is a DAO,Database object.

    AllQueries is not a property of the DAO Database object, but of Access.CurrentData.

    Use the QueryDefs collection of the DAO.Database object.

    Private Sub cmdOK_Click() 
        Dim Qdf As Object 
        Dim Dbs As Object 
        Set Dbs = OpenDatabase("D:\Folder\Test.accdb", False, False, ";pwd=" & DatabasePassword & "") 
        For Each Qdf In Dbs.QueryDefs
            Debug.Print Qdf.Name 
        Next Qdf
    End Sub
    

    Was this answer helpful?

    0 comments No comments