J
Judy Ward
Currently I have 14 individual queries in my Access database that differ only
by the criteria for one of the fields (the IR field). I have code that sends
each query to an Excel file. When a change is requested to the query (add or
delete a field) I have to edit all 14 queries.
What I would like to do is have an array of the 14 different IR's and
execute this query in a loop. I made sure the sql works when I run it for
one index of the array, but I really don't know enough about queries to get
this working in a loop. This is what I have so far (that is not working):
' Not showing the declaration of the array, etc.
Dim qry As New DAO.QueryDef
For i = 1 To 14
strSQL = "SELECT <lots of fields> WHERE myTable.IR Like '*" & IR_List(i) &
"*';"
qry.sql = strSQL
qry.Name = IR_List(i)
On Error Resume Next
DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name
On Error GoTo 0
DAO.Workspaces(0).Databases(0).QueryDefs.Append qry
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, qry.Name, filenm
Next i
It works for the first index of the array and then stops with this error:
Run-time error '3219': Invalid operation
and hilights this line in my code:
DAO.Workspaces(0).Databases(0).QueryDefs.Append qry
I would appreciate help figuring out why this is stopping here.
Thank you,
Judy
by the criteria for one of the fields (the IR field). I have code that sends
each query to an Excel file. When a change is requested to the query (add or
delete a field) I have to edit all 14 queries.
What I would like to do is have an array of the 14 different IR's and
execute this query in a loop. I made sure the sql works when I run it for
one index of the array, but I really don't know enough about queries to get
this working in a loop. This is what I have so far (that is not working):
' Not showing the declaration of the array, etc.
Dim qry As New DAO.QueryDef
For i = 1 To 14
strSQL = "SELECT <lots of fields> WHERE myTable.IR Like '*" & IR_List(i) &
"*';"
qry.sql = strSQL
qry.Name = IR_List(i)
On Error Resume Next
DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name
On Error GoTo 0
DAO.Workspaces(0).Databases(0).QueryDefs.Append qry
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, qry.Name, filenm
Next i
It works for the first index of the array and then stops with this error:
Run-time error '3219': Invalid operation
and hilights this line in my code:
DAO.Workspaces(0).Databases(0).QueryDefs.Append qry
I would appreciate help figuring out why this is stopping here.
Thank you,
Judy