chaserracer83
Nov 16, 2011, 08:40 AM
Hi, I have a project where I would like to pull information from multiple parameter queries using cells in a worksheet. My VB skills are not great, but I have been able to use code found on other forums to pull from a single query. The problem is when I try to add another query to my code it only pulls from the first query.
I am attaching two sets of code below… the first is the working code that pulls form a single query, the second is me adding code to try and pull from an additional query (does not work). The code includes turning off calculations while the import is being done and then turning them back on once the import is complete.
If anyone is able to help me figure out how to make this work it would be a huge lifesaver.
FIRST SET OF CODE, WORKS PULLING IN A SINGLE PARAMETER QUERY:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error Go to CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim I As Integer
Set MyDatabase = DBEngine.OpenDatabase _
("H:\Dashboard\ManifestData.mdb")
Set MyQueryDef = MyDatabase.QueryDefs("ManifestAnalysis")
With MyQueryDef
.Parameters("[Origin:]") = Range("D5").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("All Data - CY").Select
ActiveSheet.Range("A6:K999999").ClearContents
ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
MsgBox "The station data has been added to the workbook. Please hit OK and allow time for the calcualtions to process."
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub
SECOND SET OF CODE, MEANT TO PULL IN TWO PARAMETER QUERIES BUT ONLY PULLS IN ONE:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error Go to CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim I As Integer
Set MyDatabase = DBEngine.OpenDatabase _
("
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub
SECOND SET OF CODE, MEANT TO PULL IN TWO PARAMETER QUERIES BUT ONLY PULLS IN ONE:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error GoTo CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
Set MyDatabase = DBEngine.OpenDatabase _
(")
Set MyQueryDef = MyDatabase.QueryDefs(")
Set MyQueryDef = MyDatabase.QueryDefs(")
With MyQueryDef
.Parameters("[Origin:]")
With MyQueryDef
.Parameters("D5") = Range("All Data - CY").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("A6:K999999").Select
ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
For I = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, I).Value = MyRecordset.Fields(I - 1).Name
Next I
Set MyDatabase = DBEngine.OpenDatabase _
(").ClearContents
ActiveSheet.Range(")
Set MyQueryDef = MyDatabase.QueryDefs("ManifestAnalysisTwo").CopyFromRecordset MyRecordset
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
Set MyDatabase = DBEngine.OpenDatabase _
("[Origin:]")
Set MyQueryDef = MyDatabase.QueryDefs("D5")
With MyQueryDef
.Parameters("Station Data - CY") = Range("A6:K999999").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("A2").Select
ActiveSheet.Range("The station data has been added to the workbook. Please hit OK and allow time for the calcualtions to process."
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub
I am attaching two sets of code below… the first is the working code that pulls form a single query, the second is me adding code to try and pull from an additional query (does not work). The code includes turning off calculations while the import is being done and then turning them back on once the import is complete.
If anyone is able to help me figure out how to make this work it would be a huge lifesaver.
FIRST SET OF CODE, WORKS PULLING IN A SINGLE PARAMETER QUERY:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error Go to CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim I As Integer
Set MyDatabase = DBEngine.OpenDatabase _
("H:\Dashboard\ManifestData.mdb")
Set MyQueryDef = MyDatabase.QueryDefs("ManifestAnalysis")
With MyQueryDef
.Parameters("[Origin:]") = Range("D5").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("All Data - CY").Select
ActiveSheet.Range("A6:K999999").ClearContents
ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
MsgBox "The station data has been added to the workbook. Please hit OK and allow time for the calcualtions to process."
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub
SECOND SET OF CODE, MEANT TO PULL IN TWO PARAMETER QUERIES BUT ONLY PULLS IN ONE:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error Go to CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim I As Integer
Set MyDatabase = DBEngine.OpenDatabase _
("
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub
SECOND SET OF CODE, MEANT TO PULL IN TWO PARAMETER QUERIES BUT ONLY PULLS IN ONE:
Sub GoToManual()
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual
On Error GoTo CalcBack
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
Set MyDatabase = DBEngine.OpenDatabase _
(")
Set MyQueryDef = MyDatabase.QueryDefs(")
Set MyQueryDef = MyDatabase.QueryDefs(")
With MyQueryDef
.Parameters("[Origin:]")
With MyQueryDef
.Parameters("D5") = Range("All Data - CY").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("A6:K999999").Select
ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
For I = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, I).Value = MyRecordset.Fields(I - 1).Name
Next I
Set MyDatabase = DBEngine.OpenDatabase _
(").ClearContents
ActiveSheet.Range(")
Set MyQueryDef = MyDatabase.QueryDefs("ManifestAnalysisTwo").CopyFromRecordset MyRecordset
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
Set MyDatabase = DBEngine.OpenDatabase _
("[Origin:]")
Set MyQueryDef = MyDatabase.QueryDefs("D5")
With MyQueryDef
.Parameters("Station Data - CY") = Range("A6:K999999").Value
End With
Set MyRecordset = MyQueryDef.OpenRecordset
Sheets("A2").Select
ActiveSheet.Range("The station data has been added to the workbook. Please hit OK and allow time for the calcualtions to process."
Application.Calculation = xlCalc
Exit Sub
CalcBack:
Application.Calculation = xlCalc
End Sub