D
darees1
I am trying (and failing) to open a query which contains a parameter
(named ‘myprop’) and export the results to Excel. The problem seems to
be defining and opening the recordset with parameters. Any help would
be greatly appreciated. Here is the code:
Option Compare Database
Public Sub BillingExtract()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet
Dim App As Excel.Application
Dim WB1, WB2 As Excel.Workbook
Dim WS1, WS2 As Excel.Worksheet
Set db = CurrentDb
Set qdf = db.QueryDefs("Qry_Recent_Reads")
qdf.Parameters("[myprop]").Value = "AD"
'establish link to excel
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = Workbooks.Add
objWkb.Sheets("Sheet1").Name = "ECS Upload"
Set objSht = objWkb.Worksheets(1)
objSht.Visible = xlSheetVisible
'export query results to Excel
Set rs = qdf.OpenRecordset()
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
With objSht
.Range(.Cells(1, 1), .Cells(intMaxRow,
intMaxCol)).CopyFromRecordset rs
End With
End If
Set rs = Nothing
Set db = Nothing
Set qdf = Nothing
End Sub
(named ‘myprop’) and export the results to Excel. The problem seems to
be defining and opening the recordset with parameters. Any help would
be greatly appreciated. Here is the code:
Option Compare Database
Public Sub BillingExtract()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet
Dim App As Excel.Application
Dim WB1, WB2 As Excel.Workbook
Dim WS1, WS2 As Excel.Worksheet
Set db = CurrentDb
Set qdf = db.QueryDefs("Qry_Recent_Reads")
qdf.Parameters("[myprop]").Value = "AD"
'establish link to excel
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = Workbooks.Add
objWkb.Sheets("Sheet1").Name = "ECS Upload"
Set objSht = objWkb.Worksheets(1)
objSht.Visible = xlSheetVisible
'export query results to Excel
Set rs = qdf.OpenRecordset()
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
With objSht
.Range(.Cells(1, 1), .Cells(intMaxRow,
intMaxCol)).CopyFromRecordset rs
End With
End If
Set rs = Nothing
Set db = Nothing
Set qdf = Nothing
End Sub