Populating unbound text box on a report (1 Viewer)

ontopofmalvern

Registered User.
Local time
Today, 14:23
Joined
Mar 24, 2017
Messages
64
Hi when I run this code on an open Form the unbound text box called "txtBox" gets filled with the word "bob"

Code:
Forms!frmMyForm!txtBox = "bob"

but when I run this on an open Report

Code:
Reports!rptMyReprt!txtBox = "bob"

I get the error "Argument not optional" (also VBA editor changes "Reports!" to "reports!" which I think is a clue ..)

I have a nasty workaround for my current problem using TempVars to pass the value to the Report then having an onload event like this

Code:
Me.txtBox = TempVars!strText

There must be a better way which I feel must be dead obvious but I can't find it.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:23
Joined
Oct 29, 2018
Messages
21,485
Hi. Not in front of a computer now, so I could be wrong, but I think reports can't usually be changed once it's already rendered, not like forms, which are dynamic objects.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:23
Joined
Feb 19, 2013
Messages
16,627
rather than using tempvars, you can use the openargs parameter for docmd.openreport. AS DBG say, reports are not dynamic once opened. As you have discovered you need to use report events to modify report contents.
 

ontopofmalvern

Registered User.
Local time
Today, 14:23
Joined
Mar 24, 2017
Messages
64
Many thanks, that makes sense now and is obvious (isn't always once you know?)

Couldn't get openargs to work, I don't think I have the syntax right, if my control is called "txtBox" and report "rptMyReport"
what do I replace "??" with below?

DoCmd.OpenReport "rptMyReport", acViewReport, "", "", acNormal, "??"
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:23
Joined
Feb 19, 2013
Messages
16,627
DoCmd.OpenReport "rptMyReport", acViewReport, "", "", acNormal, "bob"

and in the form open or load event

me.txtBox=openargs
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:23
Joined
Feb 19, 2002
Messages
43,328
When your code is running IN the class module of a form or report, use "Me." to refer to controls in the form/report. Using the external reference - Forms!.... and Reports!.... just adds unnecessary overhead.

Also, keep in mind that Report View does NOT run code in all events so unless you need some particular feature of ReportView such as drill down, always open in PrintPreview for best results.
 

ontopofmalvern

Registered User.
Local time
Today, 14:23
Joined
Mar 24, 2017
Messages
64
Many thanks for all the helpful tips here. Especially useful is the difference between ReportView and PrintView, never really considered it before.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:23
Joined
Feb 19, 2002
Messages
43,328
People who like web pages, like ReportView. I don't like the absence of page breaks so I almost never use it.
 

Users who are viewing this thread

Top Bottom