需要帮助,就会出现一个问题,如果我尝试对表单控件执行带有条件的查询,它将始终导致错误“参数太少”。如果我不执行对表单控件使用条件的查询,则效果很好。请帮助我我在这里做错了。注意:有人建议让数据库在运行查询之前对每个参数进行评估,因此我将其代码包括在内,这些代码指示“新”,如下所示;
Private Sub ReportPDF_Click() ' On click Button
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
Dim Mth As String
Dim qdf As DAO.QueryDef 'new
Dim prm As DAO.Parameter 'new
mypath = "C:\PositonReport\ReportOutput\" ‘ location to store PDF report file
Set db = CurrentDb()
Set qdf = db.QueryDefs("Query2_YTD_MTH") ' 'new
For Each prm In qdf.Parameters 'new
prm = Eval(prm.Name) 'new
Next prm 'new 'new
Set rs = db.OpenRecordset("SELECT distinct [Portfolio_2] FROM [Query2_YTD_MTH]", dbOpenSnapshot)
Set rs1 = db.OpenRecordset("SELECT distinct [Period] FROM [Query2_YTD_MTH]", dbOpenSnapshot)
Do While Not rs.EOF
Mth = rs1("Period") ‘ for month name from period filed to the report file
temp = rs("Portfolio_2")
MyFileName = rs("Portfolio_2") & "-" & Mth & ".PDF"
DoCmd.OpenReport "rpt_Program", acViewReport, , "[Portfolio_2 ]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "rpt_Program"
DoEvents
rs.MoveNext
Loop
rs.Close 'close the recordset
rs1.Close
Set rs = Nothing
Set rs1 = Nothing
Set db = Nothing
Set qdf = Nothing
MsgBox "Your files have been saved to C:\PositonReport\ReportOutput\", vbOKOnly
End Sub