Sending filtered records from a form to a report.

B

Bretona10

Hi all, I want to send records filtered with Access standard filter buttons
from a form to a report so it prints just the filtered records.
Best Design Practices and code for the Print button?

Thank you
Bret
 
A

Albert D. Kallal

Bretona10 said:
Hi all, I want to send records filtered with Access standard filter
buttons
from a form to a report so it prints just the filtered records.
Best Design Practices and code for the Print button?

Thank you
Bret

You should be able to do the follwing:


docmd.OpenReport "name of reprot",,,me.Filter
 
B

Bretona10

Thanks Albert, would this apply to the records filteed on a subform? This
particular form is unbound with a subforms records matching a listbox value.
 
A

Albert D. Kallal

Bretona10 said:
Thanks Albert, would this apply to the records filteed on a subform? This
particular form is unbound with a subforms records matching a listbox
value.

You can still pass the sub-forms filter to the report.

However,, you mentioned standard filter buttions. You now have rather
differnt story here.....

You can still use this approach if listbox + code sets a filter on the
sub-form.

Any additonal filtering by the user should be added to the fitler properity.

So. If the button to launch the reprot is on the main part (not sub-form),
then go:

docmd.OpenReport "name of reprot",,,me.MySubFormContorl.Form.Filter

So, you can take a forms filter (that is the result of the user filtering)
and pass that to the report...

It not 100% clear how the initial datasource for the sub-form is setup and
restricted by the listbox....

If the filtering is JUST by the lsitbox, then simply use the same "where"
condition that you built
from the lsitbox for the report.
 
B

Bretona10

Upon closer look the main and subform are a one to many relationship. Linked
via the CNUM field, so the main form is not unbound. This changes things a
bit. When showing all the subform records if the user applies a filter by
selection in the subform, thats the records I want to print, not All the
records that are linked. Make better sense now? Sorry for the confusion.
 
A

Albert D. Kallal

Bretona10 said:
Upon closer look the main and subform are a one to many relationship.
Linked
via the CNUM field, so the main form is not unbound. This changes things a
bit. When showing all the subform records if the user applies a filter by
selection in the subform, thats the records I want to print, not All the
records that are linked. Make better sense now? Sorry for the confusion.

If the listbox is not multi-select, and the filtering is based only on "one"
value
selected in the listbox, then you should be able to do
the following:


dim strWhere as string

strWhere = "(cnum = " & me.lstBox & ")"

' now add any fitlering of the sub-form by user

if me.Mysubform.Form.Filter <> "" then
strWhere = strWhere & " and " & me.MySubForm.Form.Filter
end if

docmd.OpenReport "reprotname",acViewPreview,,strWhere

Of course, if the listbox is multi-select, then again you left out a another
big detail, and the above code would have to be changed further....

Also, if cnum is a text field, then you have add some single quotes as:
strWhere = "(cnum = '" & me.lstBox & "')"

the above assumes the button to print the report is in the main form, not
the sub-form....
 

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