MagicalHippo
Board Regular
- Joined
- Oct 13, 2015
- Messages
- 122
I'm extremely new to this, I was trying to create a parameter vba query that finds my access query and assigns values from cells to [user input] boxes
using Excel, and runs the Access query. What I am having trouble with is this is a "nested query." What I mean is this:
Q1 - grab [user input 1] [user input 2] etc..
Q2 - using inputs, filter data
Q3 - refilter data based on Work order dates
Q4 - shows difference by COUNT.
How would I tell access to Print me out Q4, but use Excel (A2,A3) cells to be a user input @ Q1?
The .Parameters in my Code are in Q1, but I point it at Q4 as I want to display the info from there..
**Apologies if this is confusing, as this is my first time creating something like this.
Ex of code:
using Excel, and runs the Access query. What I am having trouble with is this is a "nested query." What I mean is this:
Q1 - grab [user input 1] [user input 2] etc..
Q2 - using inputs, filter data
Q3 - refilter data based on Work order dates
Q4 - shows difference by COUNT.
How would I tell access to Print me out Q4, but use Excel (A2,A3) cells to be a user input @ Q1?
The .Parameters in my Code are in Q1, but I point it at Q4 as I want to display the info from there..
**Apologies if this is confusing, as this is my first time creating something like this.
Ex of code:
Code:
Sub RunParameterQuery()
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
("J:\NFI-07\Misc Reports\Data Dumps.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Snapshot of xWW")
'Step 3: Define the Parameters
With MyQueryDef
.Parameters("[Enter WW]") = Range("B1").Value
.Parameters("[Enter Scope Status]") = Range("B2").Value
.Parameters("[Enter T-NBR]") = Range("B3").Value
.Parameters("[Enter Reason Code]") = Range("B4").Value
End With
'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 5: Clear previous contents
Sheets("Sheet1").Select
ActiveSheet.Range("A6:K10000").ClearContents
'Step 6: Copy the recordset to Excel
ActiveSheet.Range("A7").CopyFromRecordset MyRecordset
'Step 7: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(6, i).Value = MyRecordset.Fields(i - 1).Name
Next i
MsgBox "Your Query has been Run"
End Sub
Last edited: