Does anyone know if there is a way to set the values of query parameters from VBA for a report's recordsource? (i.e., I want to set the values of the parameters and NOT have the user prompted for them).
Based on a Microsoft page (http://support.microsoft.com/kb/287437), I tried the following approach:
Expand|Select|Wrap|Line Numbers
- Private Sub Report_Open(Cancel As Integer)
- Dim sql As String
- Dim dbsSoftwareConfiguration As Database
- Dim qdf As QueryDef
- Dim rst As Recordset
- Dim stDocName As String
- Set dbsSoftwareConfiguration = CurrentDb
- 'if rpt form is open and "include all sites" box is NOT checked
- If myUtil.FrmIsOpen("frmReportBuilder") And (Not Forms!frmReportBuilder.includeAllSitesCheckBox) Then
- sql = "PARAMETERS [pSite_ID] Long; SELECT * FROM qryObsoleteMediaBySite;"
- Set qdf = dbsSoftwareConfiguration.CreateQueryDef("tempQry", sql)
- qdf.Parameters![pSite_ID] = Forms!frmReportBuilder.obsoleteSiteMediaComboBox
- Set rst = qdf.OpenRecordset
- Me.RecordSource = qdf.Name
- 'Me.titleTextBox = "Obsolete Media for " & _
- Forms!frmReportBuilder.obsoleteSiteMediaComboBox.Column(1)
- Else
- Me.RecordSource = "qryObsoleteMedia"
- 'Me.titleTextBox = "Obsolete Media Across All Sites"
- End If
- Set qdf = Nothing
- Set rst = Nothing
- Set dbsSoftwareConfiguration = Nothing
- End Sub
I've also tried using the following code to set the parameter directly in the query, but the user is still prompted for the parameter:
Expand|Select|Wrap|Line Numbers
- dbsSoftwareConfiguration.QueryDefs("qryObsoleteMediaBySite").Parameters("pSite_ID") = _
- Forms!frmReportBuilder.obsoleteSiteMediaComboBox
Any ideas for how to programatically set the value of a parameter for a query that is the recordsource for a report?
Thanks in advance for any ideas!
Dana