populating textbox sub report

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » Populating a textbox in a sub report using VBA | Access forum

Populating a textbox in a sub report using VBA | Access forum

resolvedResolved · Low Priority · Version 2002/XP

Joe has attended:
Access Intermediate course

Populating a textbox in a sub report using VBA

Dear Best STL

I have a report containing a subreport
The subreport grows if there are records in its 'Detail' section and shrinks to nothing when there is nothing to display - this works fine.
I want to add a title/header in the Report Header section of the subreport and populate it using VBA - I have to use VBA as I'm dealing with two languages.
I've created a Sub to do this and a reference to the Sub within the subreport's Activate event:

Sub popSubTTL()
[TextBox_my] = "my text"
End Sub

Private Sub Report_Activate()
popSubTTL
End Sub

If I open the subreport in isolation the TextBox populates fine.
If I open the main report, the records in the detail section show as they should, but the TextBox doesn't populate!
When I add a breakpoint to the above Subs, and open the main form, the above functions are completely skipped (can't even step through them) and the textbox doesn't get populated.
If I delete the Report_Activate() Sub but target the popSubTTL() from the main form, the code stops at the breakpoint and all looks as if it should work ok - the text box is assigned the text as when I rollover the [TextBox_my] part of the code I see it has the value of "my text", but the textbox just doesn't get populated!
I haven't any more ideas of how to populate this TextBox! It should be the easiest thing to do and works everywhere else, but not on this subreport.

Your help is much appreciated!
Joe

RE: Populating a textbox in a sub report using VBA

Hi Joe

Thank you for the forum question.

You are very close to do what you want.

You just need to add few more things.

Sub popSubTTL()
Reports![name of the main report]![name of the subreport]! [TextBox_my] = "my text"
End Sub

Private Sub Report_Activate()
popSubTTL
End Sub

You will not be able to see the result in report view but you can see it in print view.

I hope that this will do the job.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Edited on Mon 27 Oct 2014, 16:44

RE: Populating a textbox in a sub report using VBA

Dear Jens,

thanks for your answer - Unfortunately your suggestion still doesn’t populate the textbox.

The code I gave is part of the subreport itself (not in a separate module object) so normally it shouldn't anyway need a path to one its controls, except for perhaps "Me!" (which doesn't change anything in my case). What I don't understand is why the TextBox DOES populate if the subreport is opened in isolation, but as soon as it's part of a main report FAILS to populate?!
I know my code works at least on the SubReport level.

I must confess I don't know which view 'report view' is - I know 'design view', 'Print preview' & 'Layout Preview'. In any case the report opened by Print/Layout Preview shows the textbox populated correctly (if opened in isolation).

Do you have another suggestion?
Best regards.
Joe

RE: Populating a textbox in a sub report using VBA


Hi Joe,

Yes I did put the code

Sub popSubTTL()
Reports![name of the main report]![name of the subreport]! [TextBox_my] = "my text"
End Sub

in a module not a private module for the Report and the code below as an activate event for the main report (the main report 's private module) not the sub report.

Private Sub Report_Activate()
popSubTTL
End Sub

In my tests the textbox was populated correctly. I do not know why it is not working in your database.

I am sorry but it should work as described.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Populating a textbox in a sub report using VBA

Hi Jens,

thanks for the reply.

I'm away for a few days but will get back to you shortly afterwards.

Best regards.
Joe

RE: Populating a textbox in a sub report using VBA

Hi Jens,

Now I'm completely confused! … I wanted to create a simple report (with subreport) based on simple tables so I could test your code, but this just creates more questions which have nothing to do with my original question.
It's quite frustrating as I've put together a hugely complicated (working) data base dealing with items running into literally 100s of combinations, handling invoices, delivery lists and keeps track of invoicing etc; but when I want to make a report, I'm stuck!

I've sent the database to forum@stl-training.co.uk

1. Main report opened: Can you tell me why only one order number shows? For my understanding it should show one order number per page - 3 pages for 3 order numbers (14.10.01, 14.10.02, 14.10.03).
2. Main report opened: Can you tell me why the sub report shows only 2 records for Order number 14.10.01? There are 3 records for this order number
3. Main report opened: Can you explain why the title in the sub report 'SUB REPORT' doesn't show?
4. I've attached code to the sub report to populate a text box (my original question). This works if the sub report is opened directly but fails if the main report open. Why is this such a problem for Access?!

Best regards.
Joe

RE: Populating a textbox in a sub report using VBA

Hi Joe,

Please send the database to info@stl-training.co.uk

Thanks.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Populating a textbox in a sub report using VBA

Hi Joe,

The reports in Access is time consuming to create and to make look nice.

I have created a new main and a new sub report. If you look at the reports in design view you will see the different sections where I have added the source text boxes. I have also done the vba part so you will find a text control in each sub report.

You will have to open the report in print preview to see the text in all the sub report or print the report.

I hope my examples will guide you in the right direction to do what you need to do.

The database is attached.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Attached files...

TestPopSubReport.mdb

RE: Populating a textbox in a sub report using VBA

Dear Jens,

Thank you very much for taking the time to create an example - It's always best to learn through a working example and it helped a lot.

- I've looked at it in parallel while having to create another report (for work) & see why certain records were not showing (answering my questions 1 & 2).
- It appears (through my testing) any page headers/footers are omitted if the report becomes a Subreport. I guess this is just how Microsoft have decided Subreports should display (answering question 3 in a way).
- I've tested variations of populating a Subreport TextBox and found success when:
a) The Sub holding the VBA to populate the TextBox is in the Subreport, and pointed to from the main report
b) The Sub holding the VBA to populate the TextBox is in a separate module and pointed to from the main report
Although I can achieve what I want, I find the coding messy - I would rather keep Subreport code within its own module and not have an external 'source' trigger it (or use separate modules for that matter). I find it annoying code (within a Subreport) works when a Subreport is opened in isolation, but magically fails to work when put in another report.

Is this a bug or do you know a reason why such code fails? If no one knows - no problem, but in a sense I'd like to know, or even better, how it could be made to work.

Many thanks
Joe

RE: Populating a textbox in a sub report using VBA

Hi Joe,

I do not have any better solution.

The issue is that you want the macro to run when the main report is activated, so you need a on activate event in the main report private module.

If you you have an on activate event in the sub report the macro will only be trigged when you open the sub report not the main report. If you in the main report on activate event has the line:
reports![name of sub report].activate

To activate the sub report to trigger a on activate event in the sub report.

You will get the information "You cannot activate a report which are not open"

Sorry I cannot give you a better option.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Populating a textbox in a sub report using VBA

Hi Jens,

thanks for your explanation.

I'm happy to have a solution, even though it seems a bit of a work around, so I'll stick with it!

Have a good day.
Until next time :)
Joe

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Access tip:

Deleting duplicate records from a table

You cannot delete records tables where there duplicate records. A way around this is to create a new table which wont hold the duplicates. and then deleting the old table.

1. Use a make-table query based on this table only. IMPORTANT - Ensure that you include all of the fields from your original table or you may loose data.

2. Open the query's property sheet by using VIEW, QUERY, PROPERTIES, and set the Unique Values property to Yes

3. Because you have selected the Unique Values to Yes when you run the query, Access creates a new table without duplicate records. You can now delete the old table and rename the new one.

View all Access hints and tips


Server loaded in 0.12 secs.