2

Hopefully this is the last question I have about Access since I will be done working with it very soon.

I currently have a report that is generated from a form that is filled out when a "Run" button is pressed. A database primary key is searched for and the form is populated. When the user clicks a "Print" button, a printable report is generated, being populated by data from that form. All of that was easy.

Now, in the form, there is a single list box. That list box displays results for a transaction type depending on which transaction type is clicked. For example, clicking "Purchase" will yield all customers who have purchased something in the list box, and clicking "Sold" will yield all customers who have been sold something in the list box. There are many more transactions than those two, but that does not matter for the purpose of this question.

What I need to do is make the contents of that list box (which is populated by a query in VBA) for every transaction type appear on the printable report.

So for example, the report might look something like this:

Purchases: John Cindy Alex

Sold: Matt Steven John Rob

Does anyone know a simple way to do this? I can vaguely think of how I might do this, but it is complex and I don't know the exact functions I would need to use. Basically I was thinking I would run the query that populates the list box, but then parse through each result in a while loop, and display it on the report.

Thank you in advance for any help!

2 Answers 2

2

A Report is a fairly static object in comparison to a Form. To dynamically change the data that is displayed in a report you would typically use the Where argument for OpenReport, usually on a button press and reading some value from a control on the currently active form:

DoCmd.OpenReport "rptName", acViewPreview, , "SomeField=" & frmName!ctlName

If the value on the form is a string then it needs to be surrounded with apostrophes:

DoCmd.OpenReport "rptName", acViewPreview, , "SomeField='" & frmName!ctlName & "'"

A report also has a Filter property that you might use, or you can programmatically set its RecordSource. The first option is the most common, and the easiest.

If the report has a listbox then you would typically modify its RowSource, using a query with a criteria that refers to a control on your form. But I would question why this is necessary, as the listbox would have no relation to the rest of the data in the report.

If you are talking about dynamically changing what appears on the report - moving and hiding controls, etc. - then this is a different question. You would need to first open the report in Design View. This is unusual and rarely necessary. Typically, you would create a separate report, perhaps copying the original report, and just use the button to decide which report to display. (There are Report Events that can be used to hide, or move, controls on the report - OnFormat, for example - that wouldn't require the report to be opened in Design View, but I don't think this is what you are looking for.)

0

Assuming that the report will be created using the recordset that was used to create the form, you could join the recordset that populates the listbox to the recordset that populates the rest of the form. If you, say, LEFT JOIN the recordset for the list box to the recordset for the form on the transaction type, the resulting recordset should have every thing you need to create the report required. If the report needs to chabnge its format based on the transaction type, try using the OnOpen and OnClose Events for the report and the OnFormat Event for the Detail sections. I have some experience with reports like that. If you need more help with the events, add comments with some specific detail and I can probably make useful suggestions.

1
  • Could you give a simple example on how to do this? Aug 5, 2013 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.