Hi there,
I am trying to create a module with VBA to output approx 500 access reports into a folder. The access report is linked to a query called “qryRptS” with each record in the query responsible for one report. The problem is that qryRptS is a parameterised query based on 2 date fields, (Forms!frmCompany![StartDate] and Forms!frmCompany![EndDate]), which are entered on a form.
The code I have produced returned the error “Too few parameters, expected 2”, which led me to believe that VBA doesn’t like parameterised queries and needs to have exact parameter values supplied in the code. I think this can be done using something called QueryDef objects but as I don’t have much experience with VBA I wonder whether anyone could provide some guidance.
Here is what I have so far. I think I need to incorporate the lines in bold-italic below but I don’t really understand this part. Currently this code is generating the error: “Data type mismatch in criteria expression”. Please let me know if you have any thoughts…
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("qryRptS")
qdf![Forms!frmCompany!StartDate] = #1/1/2011#
qdf![Forms!frmCompany!EndDate] = #1/1/2012#
strSQL = "Select * FROM qryRptS "
Set rst = qdf.OpenRecordset(dbOpenDynaset)
'For Each prm In qdf.Parameters
' prm.Value = Eval(prm.Name)
'Next prm
filename = Forms!frmCompany!TradingName & Forms!frmCompany!CompanyID
filename = Replace(filename, "/", "-")
rst.MoveFirst
Do While rst.EOF = False
DoCmd.OpenReport "rptQryS", acViewReport, "", "", acNormal
DoCmd.OutputTo acOutputReport, "rptQryS", "PDFFormat(*.pdf)", "\\HBSRV01\common\PROJECTS\Q1 Test\" & filename & ".pdf", False, "", , acExportQualityPrint
DoCmd.Close acReport, "rptQryS"
rst.MoveNext
Loop
ReportGeneration_Exit:
Exit Function
ReportGeneration_Err:
MsgBox Error$
Resume ReportGeneration_Exit
End Function
I am trying to create a module with VBA to output approx 500 access reports into a folder. The access report is linked to a query called “qryRptS” with each record in the query responsible for one report. The problem is that qryRptS is a parameterised query based on 2 date fields, (Forms!frmCompany![StartDate] and Forms!frmCompany![EndDate]), which are entered on a form.
The code I have produced returned the error “Too few parameters, expected 2”, which led me to believe that VBA doesn’t like parameterised queries and needs to have exact parameter values supplied in the code. I think this can be done using something called QueryDef objects but as I don’t have much experience with VBA I wonder whether anyone could provide some guidance.
Here is what I have so far. I think I need to incorporate the lines in bold-italic below but I don’t really understand this part. Currently this code is generating the error: “Data type mismatch in criteria expression”. Please let me know if you have any thoughts…
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("qryRptS")
qdf![Forms!frmCompany!StartDate] = #1/1/2011#
qdf![Forms!frmCompany!EndDate] = #1/1/2012#
strSQL = "Select * FROM qryRptS "
Set rst = qdf.OpenRecordset(dbOpenDynaset)
'For Each prm In qdf.Parameters
' prm.Value = Eval(prm.Name)
'Next prm
filename = Forms!frmCompany!TradingName & Forms!frmCompany!CompanyID
filename = Replace(filename, "/", "-")
rst.MoveFirst
Do While rst.EOF = False
DoCmd.OpenReport "rptQryS", acViewReport, "", "", acNormal
DoCmd.OutputTo acOutputReport, "rptQryS", "PDFFormat(*.pdf)", "\\HBSRV01\common\PROJECTS\Q1 Test\" & filename & ".pdf", False, "", , acExportQualityPrint
DoCmd.Close acReport, "rptQryS"
rst.MoveNext
Loop
ReportGeneration_Exit:
Exit Function
ReportGeneration_Err:
MsgBox Error$
Resume ReportGeneration_Exit
End Function