473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Setting query parameters from VBA for a Report

12
Hello Experts!

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
  1. Private Sub Report_Open(Cancel As Integer)
  2.  
  3.     Dim sql As String
  4.     Dim dbsSoftwareConfiguration As Database
  5.     Dim qdf As QueryDef
  6.     Dim rst As Recordset
  7.     Dim stDocName As String
  8.  
  9.     Set dbsSoftwareConfiguration = CurrentDb
  10.  
  11.     'if rpt form is open and "include all sites" box is NOT checked
  12.     If myUtil.FrmIsOpen("frmReportBuilder") And (Not Forms!frmReportBuilder.includeAllSitesCheckBox) Then
  13.  
  14.         sql = "PARAMETERS [pSite_ID] Long; SELECT * FROM qryObsoleteMediaBySite;"
  15.         Set qdf = dbsSoftwareConfiguration.CreateQueryDef("tempQry", sql)
  16.         qdf.Parameters![pSite_ID] = Forms!frmReportBuilder.obsoleteSiteMediaComboBox
  17.  
  18.         Set rst = qdf.OpenRecordset
  19.         Me.RecordSource = qdf.Name
  20.  
  21.         'Me.titleTextBox = "Obsolete Media for " & _
  22.             Forms!frmReportBuilder.obsoleteSiteMediaComboBox.Column(1)
  23.  
  24.     Else
  25.  
  26.         Me.RecordSource = "qryObsoleteMedia"
  27.         'Me.titleTextBox = "Obsolete Media Across All Sites"
  28.     End If
  29.  
  30.     Set qdf = Nothing
  31.     Set rst = Nothing
  32.     Set dbsSoftwareConfiguration = Nothing
  33. End Sub
  34.  
The problem is that setting the recordsource instead of the recordset appears to rerun the query, ignoring the parameter values passed in -- resulting in the standard prompt to the user. Setting the recordset directly is apparently not an option for reports (based on the Microsoft page linked to above). (Note: I have used this method and set the recordset property directly for forms, and it does work in that situation).

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
  1. dbsSoftwareConfiguration.QueryDefs("qryObsoleteMediaBySite").Parameters("pSite_ID") = _
  2.             Forms!frmReportBuilder.obsoleteSiteMediaComboBox
  3.  
Because I am using the same saved queries in multiple places, I'd rather not bind the criteria in the query directly to the form. If I do that, I have to save a different version of the same query for each form or report I want to use it with.

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
Apr 10 '07 #1
5 5271
Denburt
1,356 Expert 1GB
Looks like your taking an interesting approach, sorry I don't have time right now to test out the code you are using. I have one instance where I have to change the query to set my criteria so in the reports on close event I stipulate a clean up function that returns the query to it's original state and the criteria no longer exists (just an idea).
Apr 11 '07 #2
dana1
12
That makes sense.. So instead of trying to set the parameter value, I can just access the .sql property of the query def and replace the "parameter" part of the string with a reference to the appropriate control on form. That might actually work.

I'll play around with it this afternoon and let you know. Thanks!
Apr 11 '07 #3
Denburt
1,356 Expert 1GB
Your quite welcome glad we could help. Sounds like your on the right track. Let us know how it goes.
Apr 11 '07 #4
dana1
12
Your suggestion worked quite well. I wrote a little "find and replace" type of function, and just before I open the report I use it on the string in the .sql property of the saved query to replace any instances of my parameter ("[pSite_ID]") with a reference to the control on the form ("[Forms]![frmReportBuilder].[obsoleteSiteMediaComboBox]"). Then after I run the open report command, I use the "find and replace" function again to change the references to the control back to the parameter name.

This means that I can get rid of all my querydef stuff in the open event of the form-- I don't need it anymore.

Works like a champ!

I had it in my head that there had to be a way to pass parameter values to the query, so it didn't even occur to me to just change the sql statement :-) Thanks for the idea!

Dana
Apr 11 '07 #5
Denburt
1,356 Expert 1GB
Your quite welcome, if you need anything else give us a holler.
Apr 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
4
by: Andy Davis | last post by:
I have developed a number of reports that are based on parameter queries where the user enters criteria such as a date range and a sales rep say. I want to be able to show a graphical picture in...
3
by: Bryan | last post by:
I've been looking over this newsgroup, but I can't find an answer to my problem. I see that a few others have posted this issue over the years, but none of them got a response. I'll give it...
8
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing...
6
by: jim | last post by:
Is anyone able to provide me with a link to useful documentation or just outright explain to me how to set query parameters dynamically? I'm really new to Access and databases in general but I...
5
by: John | last post by:
Hi I have a parameterised query for a report like this; PARAMETERS Event_ID Short; SELECT DISTINCTROW Events., ... FROM Events WHERE (((Events.)=)); Now I am trying to run the report...
6
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.