How to create a report based on used selected query

F

FSHOTT

Can someone tell me how I can create a Report which is base on a query where
users can select from list controls the field values for the query. I have a
Purchasing Table which contains date, supplier, part number plus a few other
fields. I would like a user to be able to select from list controls the YEAR,
SUPPLIER ID AND MONTH to create a query which can be used in a report for
these specific parameters. Thanks....
 
B

Bob Quintal

Can someone tell me how I can create a Report which is base on a
query where users can select from list controls the field values
for the query. I have a Purchasing Table which contains date,
supplier, part number plus a few other fields. I would like a user
to be able to select from list controls the YEAR, SUPPLIER ID AND
MONTH to create a query which can be used in a report for these
specific parameters. Thanks....


First crate an unbound form with combo or list boxes (combo is
easier) for year, month and supplier ID. Each should have a rowsource
of the distinct values from the appropriate column of the table.

Next create a query that returns all rows of the columns you want in
the report.

Third create the report that shows all the rows, based on the query.

Fourth, On the form, add an Open Report button, using the Wizard.

Fifth, select the button, open the properties box, choose the events
tab, click in the On Click event (it should already say event
procedure) then the elipse (...) button to go into the vba editor.

You will see a line: Dim stDocName As String
add a new line under that Dim stWhereClause As String
and underneath that one lines that say something like

stWhereClause = " year([PurchaseDate]) = " & me.combo1
stWhereClause = stWhereClause & " Month([PurchaseDate]) = " &
me.combo2
stWhereClause = stWhereClause & " [SupplierID] = " & me.combo2

You'll need to change the names of the combo boxes and the fields to
match your particular situation.

Now find the line DoCmd.OpenReport stDocName, acPreview
We must add the filtering to this line:

DoCmd.OpenReport stDocName, acPreview, ,stWhereClause

Save everything and test.
 
F

FSHOTT

Bob and Ken Thanks you for your responses. I have a couple of questions about
the direction you provided. In my form the Record Source is the SELECT query
per your note. I also put this Select query in the Report record source. Is
the correct? I added a control button to the form to open the report. When I
click on the button I get the report but it is for all the Purchasing Table
data not for just the field parameters I selected from the combo controls.
When I added the stWhereClause to the VBA OpenReport code I get an error
message " Syntax error (comma) in queryexpression ' ( Year([Date])=2008,
Month([Date]) = April, [SupplierNo]=A12790)' ". I tried this without the
comma's but that did not work either. My fileds in the Purchasing Table are
named MyYear & MyMonth. Should this be what I use in the stWhereClause?
Thanks.....
 
A

Armen Stein

With Bob's method the opposite applies; you don't need to include any
parameters in the query at all. What's missing in your string expression are
the 'And' operators to tack the three criteria together.

To elaborate on Bob's method: The report can prompt the user for
criteria before it runs is to open a form from the report's Open
event. Open the form in Dialog mode so that the report waits for the
form to be closed or hidden before it proceeds. That way you can
collect criteria from the user and build a Where clause for the
report. It also means that you can call the report directly - you
don't need to call it from a form. And the selection form is reusable
- it can be called from multiple reports if they need the same
criteria.

I've posted examples of this technique at
www.JStreetTech.com/downloads - see "Report Selection Techniques".

Also, this includes code to automatically replace the Where clause in
a SQL statement.

Look in basSQLTools, specifically the function ReplaceWhereClause. You
send in a whole SQL statement and the new desired Where clause, and it
locates and snips out the old one, inserts your new one, and gives you
back the new statement. It's one of the most useful functions we've
ever written - it's used in every single application we build. It
works with both Access and SQL Server query syntax.

Rebuilding a whole SQL Statement in code is laborious and often leads
to hard-to-diagnose errors. Our technique allows you to store the
whole query structure in a property or saved query without having to
rebuild it each time in code. All you have to do is rebuild the Where
clause.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
F

FSHOTT

Bob, Ken and Armen Thank you very much for your help and guidance. Both
methods worked Great!
--
frank-a


FSHOTT said:
Bob and Ken Thanks you for your responses. I have a couple of questions about
the direction you provided. In my form the Record Source is the SELECT query
per your note. I also put this Select query in the Report record source. Is
the correct? I added a control button to the form to open the report. When I
click on the button I get the report but it is for all the Purchasing Table
data not for just the field parameters I selected from the combo controls.
When I added the stWhereClause to the VBA OpenReport code I get an error
message " Syntax error (comma) in queryexpression ' ( Year([Date])=2008,
Month([Date]) = April, [SupplierNo]=A12790)' ". I tried this without the
comma's but that did not work either. My fileds in the Purchasing Table are
named MyYear & MyMonth. Should this be what I use in the stWhereClause?
Thanks.....
--
frank-a


FSHOTT said:
Can someone tell me how I can create a Report which is base on a query where
users can select from list controls the field values for the query. I have a
Purchasing Table which contains date, supplier, part number plus a few other
fields. I would like a user to be able to select from list controls the YEAR,
SUPPLIER ID AND MONTH to create a query which can be used in a report for
these specific parameters. Thanks....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top