473,405 Members | 2,279 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,405 software developers and data experts.

Setting a parameter for a report based on a query

3
Hi all,

I'm facing an issue with Access that I haven't been able to find a solution for, despite hours of googling, so I hope someone here can help!

I've got a report - rptClientsByCountry - which is based on a query - qryClientsByCountry - that takes a parameter, myCountry, to filter the results. I need to be able to generate this report depending on the user's choice of country, preferably from a drop-down list.

Leaving aside for a minute the form part, I can't figure out how to pass a string to the report such that it accepts it as the parameter to the underlying query. Psuedo code would be:

sub command_click()

get current value of country drop down

set parameter myCountry on report

DoCmd.OpenReport "rptClientsByCountry", acViewPreview

end sub

Can this be done? Or must I resort to setting the Where condition on the DoCmd.OpenReport?

Thanks in advance!
Jun 6 '07 #1
12 10794
maxamis4
295 Expert 100+
It works just like a form

Expand|Select|Wrap|Line Numbers
  1.  
  2.     stDocName = "frm Name"
  3.  
  4.     stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
  5.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  6.  
Jun 6 '07 #2
jamjar
50
It works just like a form

Expand|Select|Wrap|Line Numbers
  1.  
  2.     stDocName = "frm Name"
  3.  
  4.     stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
  5.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  6.  
This method implies you get rid of the parameters per se.
You can pass parameters to the query in code, but I haven't figured out how to use these to run a Select query. For an action query something like this works:
Expand|Select|Wrap|Line Numbers
  1.   Dim qdf As QueryDef
  2.     Set qdf = CurrentDb.QueryDefs("yourQuery")
  3.     qdf.Parameters("myCountry").Value = Me.cmbCountry
  4.     qdf.Execute
  5.     qdf.Close
  6.     Set qdf = Nothing
  7.  
but if you try to Execute a select query it doesn't work. Ditto if you replace the execute with DoCmd.OpenReport you still receive a prompt for the parameter.
So like karanj I'm curious if anyone knows if/how you can pass a parameter to a select query so the user does not receive the prompt?

James
Jun 6 '07 #3
karanj
3
This method implies you get rid of the parameters per se.
...
if you replace the execute with DoCmd.OpenReport you still receive a prompt for the parameter.
So like karanj I'm curious if anyone knows if/how you can pass a parameter to a select query so the user does not receive the prompt?
Yep, exactly - this kind of basic modularisation would be very helpful! The "official" solution that I've found however is a little disheartening - see this article on the office.microsoft.com site. Surely that means that the query-report-form will effectively be coupled quite tightly?
Jun 7 '07 #4
karanj
3
It works just like a form

Expand|Select|Wrap|Line Numbers
  1.  
  2.     stDocName = "frm Name"
  3.  
  4.     stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
  5.     DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  6.  
here effectively the form has to "know" the table/sql - I'd prefer to avoid this if possible so that the queries can be built by one person and the form/report by another. This seems to me something rather straightforward...
Jun 7 '07 #5
jamjar
50
Yep, exactly - this kind of basic modularisation would be very helpful! The "official" solution that I've found however is a little disheartening - see this article on the office.microsoft.com site. Surely that means that the query-report-form will effectively be coupled quite tightly?
I would think so. The query would only be useful for the one form. I guess that means whatever form you may be starting with, you need to call the query's parameter form up before running the report based on it, and populate the parameter form with any selections that may already have been made in the originating form?

James
Jun 7 '07 #6
maxamis4
295 Expert 100+
Let it stand that stlinkcriteria can be defined to what ever you want

here is an example

Expand|Select|Wrap|Line Numbers
  1.        stLinkCriteria = "[fID]=" & Me![fID]
  2.  
as well you could use a recordset
Jun 7 '07 #7
jamjar
50
Let it stand that stlinkcriteria can be defined to what ever you want

here is an example

Expand|Select|Wrap|Line Numbers
  1.        stLinkCriteria = "[fID]=" & Me![fID]
  2.  
Agreed.
There have been times when I have wished to be able to use parameters instead, but I can't remember a good example.
Jun 8 '07 #8
cfs
1
One sulution I think:

Create an invisible text-field in the active form (assuming you use a form to invoke the VBA-code, or have an active form that stays open). Use the VBA-code to write the desired parameter to the text-field (be sure to to this the right place regarding loops etc).

In the query, assign the parameter in the query to the invisible text-field. As you selection changes, so does the text-field, and the query based upon it shoul return the right results. at least this worked for me :)
Mar 17 '08 #9
truthlover
107 100+
It works just like a form

Expand|Select|Wrap|Line Numbers
  1.  
  2. stDocName = "frm Name"
  3.  
  4. stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
  5. DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  6.  
What am I doing wrong? This is my modification of the code, but I'm getting a "Compile Error: Variable not defined" for both the stDocName and stLinkCriteria.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintSelected_Click()
  2.     stDocName = "rpt_SurveyWorkOrder"
  3.  
  4.         stLinkCriteria = "[tbl_SurveyWorkOrder.SurveyWorkOrderID]=" _
  5.         & Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
  6.         DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  7. End Sub
Sorry if this is a dumb question, I'm only just learning VBA

Thanks!
May 22 '08 #10
truthlover
107 100+
Not sure if anyone saw this since it was the holiday weekend:
What am I doing wrong? This is my modification of the code, but I'm getting a "Compile Error: Variable not defined" for both the stDocName and stLinkCriteria.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintSelected_Click()
  2.     stDocName = "rpt_SurveyWorkOrder"
  3.  
  4.         stLinkCriteria = "[tbl_SurveyWorkOrder.SurveyWorkOrderID]=" _
  5.         & Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
  6.         DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
  7. End Sub
Sorry if this is a dumb question, I'm only just learning VBA

Thanks!
May 27 '08 #11
maxamis4
295 Expert 100+
Not sure if you ever solved this but it looks like you are seetin your survey ID equal to the table. Which really has no reference. What you would need to do is set your report workid field what ever the name equal to the form survey id number.

Example

stLinkCriteria = "[Report.SurveyWorkOrderID]=" _
& Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
Jun 22 '08 #12
truthlover
107 100+
Yes, I did get help on that. Thanks for replying!

Not sure if you ever solved this but it looks like you are seetin your survey ID equal to the table. Which really has no reference. What you would need to do is set your report workid field what ever the name equal to the form survey id number.

Example

stLinkCriteria = "[Report.SurveyWorkOrderID]=" _
& Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
Jun 30 '08 #13

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

Similar topics

2
by: MJ | last post by:
Hi, I'm hoping this is relatively easy. I have a report based on a query - when you run the report, a form opens up and you are prompted for a date range. These are combo boxes (ie. January...
1
by: MJ | last post by:
I'm not following... where do I put that? I put a textbox on my report and put the following property for it: =!!.Value This displays 1 (for January 2003). How do I get it to display...
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...
6
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor...
5
by: dana1 | last post by:
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...
1
by: waltnixon | last post by:
I've got an MS Access query which runs fine when double clicked and returns all of the rows in a test database I'm building. I've set up a multi group report based on the query. I immediately...
2
by: PotatoChip | last post by:
I am working in Access 2002 and I have been asked to create a form for a report based on a paramter query so that the user selects the department from a drop down list on the form, query runs and...
1
by: HSXWillH | last post by:
I have looked for some help on this and this article/thread was as close to what I was looking for as I could find. http://bytes.com/forum/thread603918-Null+Parameter+Query.html My only...
25
by: DanicaDear | last post by:
I am trying my hand at my first ever Access report. It is based on a two parameter query (start date, end date). When I use the report wizard and click "view report" the report prompts me for the...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.