The following VBA Function can be used to produce a list of all the queries within a given MS Access database.
'---------------------------------------------------------------------------------------
' Procedure : ListDbQrys
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Returns a ';' separated string containing the names of all the queries
' within the database (use Split() to convert the string to an array)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sDelim : optional delimiter to used when building the list of queries. If omitted
' will default to using ';'.
'
' Usage:
' ~~~~~~
' ? ListDbQrys
' ? ListDbQrys("~")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2007-Nov-?? Initial Release
' 2 2018-09-27 Updated Copyright
' Updated Error Handler
' Minor code update
' Added optional sDelim input variable
'---------------------------------------------------------------------------------------
Function ListDbQrys(Optional sDelim As String = ";") As String
On Error GoTo Error_Handler
Dim oDbO As Access.AccessObject
Dim oDbCD As Object
Dim sQrys As String
Set oDbCD = Application.CurrentData
For Each oDbO In oDbCD.AllQueries
sQrys = sQrys & sDelim & oDbO.Name
Next oDbO
If sQrys <> "" Then
sQrys = Mid(sQrys, 2) 'Truncate initial ;
End If
ListDbQrys = sQrys
Error_Handler_Exit:
If Not oDbO Is Nothing Then Set oDbO = Nothing
If Not oDbCD Is Nothing Then Set oDbCD = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: ListDbQrys" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
This:
Qrys = Mid(Qrys, 2)
seems more efficient than
Qrys = Right(Qrys, Len(Qrys) – 1) ‘Truncate initial ;
Thanks, btw, it saved me having to work it out 🙂
Thank you, this is very helpful
Another way of getting a list of all the object in your access database, with a query:
SELECT MSysObjects.Name, MSysObjects.Type, MSysObjects.Flags, MSysObjects.DateCreate, MSysObjects.DateUpdate, IIf([type]=-32768 And [flags]=0,”Form”,IIf([type]=-32764 And [flags]=0,”Report”,IIf([Type]=4,”Table”,IIf([Type]=5,”Query”,IIf([Type]=6,”Access table”,IIf([Type]=-32761,”Modul”,””)))))) AS Its, MSysObjects.Connect
FROM MSysObjects
WHERE (((MSysObjects.Type)3) AND ((MSysObjects.name) Not Like ‘Msys*’) AND (([type]=5 And [flags]=3)=False))
ORDER BY MSysObjects.Type, MSysObjects.Flags, MSysObjects.name;
Yes, querying the MSysObjects system table can provide you information on any object, including queries. I discussed it in my post MS Access – Listing of Database Objects (Tables, Queries, Forms, Reports, …).