What is the best way to use MS Query to query against an Access Table to return the data from a range of cells as a parameter for a field in the query.
Example: I would like each of the values for say sheet(1).Range(a1:a10) to be used as a parameter on the Access table, return the row of data and then move to the next cell(a2) as the next parameter and return the results to the next row down on the sheet with the query results.
When I select a range of cells at the prompt it says I can only select a single cell as a parameter. I'm looking for something like and In statement or a For Each type of set up.
I am just learning about MS Query so I'm looking for some helpful suggestions.
Inappropriate post?
If so, Red Flag it!
Check out the FAQ
area for this forum!
SkipVought (Programmer) 17 Jan 11 20:09
Hi,
You cannot use the PARAMETER feature of MS Query is you have a LIST, as the SQL statement is different in these instances, ie
CODE
Where FieldName = ?
CODE
vs
Where FieldName IN (YourList)
It can only be done with VBA (macro) code.
If your list is STATIC, U can give you a simple function that produces a list that you can PASTE into your query SQL code ONE TIME.
However, it appears that you want to provide a dynamic list to your query. If that's the case, you'll need a procedure to run whenever your list changes. In that event, here's what you need to do.
TURN ON your macro recorder.
EDIT your query thru to the MS Query grid
File > Return data to Excel from MS Query.
Turn off your macro recorder.
COPY your recorded code and post your question and recorded code in forum707: VBA Visual Basic for Applications (Microsoft).
Skip,
I created a new macro as Skip suggested
But I'm not sure how to make it loop through a range of parameters.
Example: I would like each of the values for say sheet(1).Range(a1:a10) to be used as a parameter on the Access table, return the row of data and then move to the next cell(a2) as the next parameter and return the results to the next row down on the sheet with the query results.
When I select a range of cells at the prompt it says I can only select a single cell as a parameter. I'm looking for something like and In statement or a For Each type of set up.
I am just learning about MS Query so I'm looking for some helpful suggestions.
Inappropriate post?
If so, Red Flag it!
Check out the FAQ
area for this forum!
SkipVought (Programmer) 17 Jan 11 20:09
Hi,
You cannot use the PARAMETER feature of MS Query is you have a LIST, as the SQL statement is different in these instances, ie
CODE
Where FieldName = ?
CODE
vs
Where FieldName IN (YourList)
It can only be done with VBA (macro) code.
If your list is STATIC, U can give you a simple function that produces a list that you can PASTE into your query SQL code ONE TIME.
However, it appears that you want to provide a dynamic list to your query. If that's the case, you'll need a procedure to run whenever your list changes. In that event, here's what you need to do.
TURN ON your macro recorder.
EDIT your query thru to the MS Query grid
File > Return data to Excel from MS Query.
Turn off your macro recorder.
COPY your recorded code and post your question and recorded code in forum707: VBA Visual Basic for Applications (Microsoft).
Skip,
I created a new macro as Skip suggested
Code:
Sub Macro2()
'
' Macro2 Macro
'
'
With ActiveSheet.ListObjects.Add(SourceType:=0, Destination:=Range("$A$1")) _
.QueryTable
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_MS_Access_Database"
.Refresh BackgroundQuery:=False
End With
End Sub
But I'm not sure how to make it loop through a range of parameters.