Hi Ananth,
It's not clear to me whether or not your queries are all considered action
queries, but here is a procedure that should get you started for the most
common query types in Access. First, set a reference to the DAO Object
Library if it is not already set. Use version 3.6 for Access 2000/2002/2003,
or version 3.51 for Access 97. To check whether or not you already have this
reference set, open a new standard module. Then click on Tools > References.
If you find it checked, then just click on OK to dismiss this dialog. If not,
scroll down the list until you find it, and place a check mark in it to
select it. Then dismiss the references dialog box.
Copy the following code and paste it into your new module.
Notes:
1.) I removed the indentation from the SELECT Case, to help prevent word
wrap in a newsgroup message from splitting a line of code into two lines.
2.) The name of the table referenced is: tblQueries
The names of the queries are in a field named: QueryName
3.) I included a numeric field, indexed unique (no duplicates), which allows
one to specify a sort order. This field is named: RunOrder
This gives you the ability to easily change the order that queries are run,
without having to rename them to match a -number naming convention.
After pasting the code, click on Debug > Compile ProjectName, where
ProjectName is the name of your VBA project (likely the same name as your
database). Fix any compile errors before trying to do anything else.
To run the code, have your blinking mouse cursor anywhere within the
procedure. Then press the F5 button.
Option Compare Database
Option Explicit
Sub RunSavedQueries()
On Error GoTo ProcError
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strQueryName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT QueryName " _
& "FROM tblQueries ORDER BY RunOrder")
With rs
Do Until (.BOF Or .EOF) = True
strQueryName = rs("QueryName")
Set qdf = db.QueryDefs(strQueryName)
Debug.Print strQueryName, qdf.Type
Select Case qdf.Type
Case 0, 16, 128 'Select queries: 0=Select, 16=Crosstab, 128=Union
DoCmd.OpenQuery strQueryName
Case 32, 48, 80 'Action queries: 32=Delete, 48=Update/Append, 80=Make Table
db.Execute strQueryName, dbFailOnError
Case Else
'Do nothing for the present time.
End Select
rs.MoveNext
Loop
End With
ExitProc:
'Cleanup
On Error Resume Next
Set qdf = Nothing
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure RunSavedQueries..."
Resume ExitProc
End Sub
Tom Wickerath
Microsoft Access MVP
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________