Openreport Method and filtering

  • Thread starter Thread starter Bretona10
  • Start date Start date
B

Bretona10

Hi all, I have a form with a print button on it. sometimes I filter the forms
records using the built in Access toolbar buttons. How can I peview the
report based on the current filter in the Print button's On Click method?

Thanks,
Bret
 
Hi all, I have a form with a print button on it. sometimes I filter the forms
records using the built in Access toolbar buttons. How can I peview the
report based on the current filter in the Print button's On Click method?

Thanks,
Bret

DoCmd.OpenReport "ReportName", acViewPreview, , Me.Filter
 
Thanks, what should the report data be based on, Right now its on a Query
using one of the forms fields as criteria. If I base the report on the table
itself will it show all records if here is no filter applied, which will not
work.

Basically, I wan the report to print only the records shown on the form at
that given moment
 
Thanks, what should the report data be based on, Right now its on a Query
using one of the forms fields as criteria. If I base the report on the table
itself will it show all records if here is no filter applied, which will not
work.

Basically, I wan the report to print only the records shown on the form at
that given moment

That's fine. But that's not what you asked for.
You asked to preview the report based upon the current filter, which
is not necessarily the same as basing the report on the one record
shown on the form.

First create a report that displays all of the data you want to show.

Then¡K.

Your table should have a unique prime key field.
In my example it is named [RecordID].

On the command button's property sheet write
[Event Procedure]
on the Click event line.
Then click on the little button with 3 dots that will appear on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview , , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.

See VBA Help files for:
Where Clause + Restrict data to a subset of records
 
Hi thanks, this works for all the records that match the key field. I should
tell you that the form has a subform and the report is based on the subform
table . When I filter the subform records I want to have the report show only
those records.

fredg said:
Thanks, what should the report data be based on, Right now its on a Query
using one of the forms fields as criteria. If I base the report on the table
itself will it show all records if here is no filter applied, which will not
work.

Basically, I wan the report to print only the records shown on the form at
that given moment

That's fine. But that's not what you asked for.
You asked to preview the report based upon the current filter, which
is not necessarily the same as basing the report on the one record
shown on the form.

First create a report that displays all of the data you want to show.

Then….

Your table should have a unique prime key field.
In my example it is named [RecordID].

On the command button's property sheet write
[Event Procedure]
on the Click event line.
Then click on the little button with 3 dots that will appear on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview , , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.

See VBA Help files for:
Where Clause + Restrict data to a subset of records
 
Back
Top