Solved

How to create or change a query in code for a report

Posted on 2014-02-19
4
419 Views
Last Modified: 2014-02-20
In code, I need to change the wording in the data source query in a report.   The Report's "built-in" query  specifies a range of dates for the Transaction Date field.  Can I just create a new SELECT clause and assign it to a string variable and use this in the Filter section of a DoCmd.OpenReport command, thus...

DoCmd.OpenReport "Transactions Report", acViewPreview, NewStringVariable, "HHID = " & ClientNowPrinting

...or do I need to create the query and use the query name in the DoCmd statement?

The text of the query is so long, I wish I could use code to just change the criteria for one field in the design mode some way.
0
Comment
Question by:David_W_R
  • 2
  • 2
4 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39871740
You can supply a WHERE clause to the OpenReport method:

DoCmd.OpenReport "YourReport", acViewPreview, , "Date1=#01-01-2014#"

But your query will return only those dates as specified by the "range of dates for the Transaction Date" field.

If you need to change that, you can change the SQL property of the QueryDef:

Dim qdf As DAO.QueryDef
Set qdf = Currentdb.QueryDefs("YourReportQuery")

qdf.SQL = "Your SQL Here"

But you'd have to include the full, valid SQL query syntax.
0
 

Author Comment

by:David_W_R
ID: 39871764
Thanks for the input.   I won't get to test it until tomorrow.  By "change the SQL property" I believe you mean edit the (very complex) query and re-attach it to the Report.

So if I instead use a Where clause in the open statement, does it then return a subset of the original Report criteria  (like a filter applied to a filtered dataset) ?
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39871837
By "change the SQL property" I believe you mean edit the (very complex) query and re-attach it to the Report.
Essentially, yes. You'd change the SQL of that query via code by setting the .SQL property as shown, and then run your report (which would use the new SQL of the query).
So if I instead use a Where clause in the open statement, does it then return a subset of the original Report criteria  (like a filter applied to a filtered dataset) ?
Yes. If your original query returns all Dates > 01-01-2013, and you then filter for Dates < 12-30-2012, your report would return no records.
0
 

Author Closing Comment

by:David_W_R
ID: 39874652
Thanks for the guidance.  I'm now going to post another question asking what's wrong with my next try
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Filter cbo on datasheet - 11 31
Ms Access VBA Variables 6 26
Help with DoEvents 8 25
Access 2007 - Create Query That Returns One Record Per Customer 4 28
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question