I am trying to use VBA to pull data from access into excel. I am able to use this script for other query's, but when for this specific query, I get this parameter error. The break happens here:
Set rs = A.CurrentDb().QueryDefs("Query1").OpenRecordset()
Sub test()
Dim ws As Worksheet
Dim A As Object
Dim rs As Object
Application.DisplayAlerts = False
Set A = CreateObject("Access.Application")
Set ws = ThisWorkbook.Sheets("Sheet1")
A.Visible = True
A.OpenCurrentDatabase ("Y:\UW\B\201811.accdb")
A.DoCmd.OpenQuery ("Query1")
Set rs = A.CurrentDb().QueryDefs("Query1").OpenRecordset()
If Not rs.EOF Then
ws.Range("A2").CopyFromRecordset rs
End If
For iCols = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
rs.Close
Application.DisplayAlerts = True
End Sub
Here is query1:
SELECT W.MED, W.RX, W.HSAHRA, W.ACR, W.ML, Sum(W.MBRS) AS
SumOfMBRS, Sum(W.PREM) AS SumOfPREM, Sum(W.CLMSTAT) AS SumOfCLMSTAT
FROM W
WHERE (((W.[DATE]) Between [Enter Start Date:] And [Enter End Date:]) AND
((W.[ST])=[Enter State:]))
GROUP BY W.MED, W.RX, W.HSAHRA, W.ACR, W.ML
ORDER BY W.MED, W.RX, W.HSAHRA, W.ACR, W.ML;
Also, how do i convert query1 into a sqlstring?