Solved

Programmatically set text in textbox on Access Report

Posted on 2003-11-30
12
1,925 Views
Last Modified: 2008-02-01
I have a report which is loaded by clicking a button on a form. The subtitle field should contain the same text as a label on the form. Currently I'm linking this textbox directly to the contents of that label, but then it generates an error if the form is closed (or not open in the first place, that is if the report is opened directly). I would like to avoid this by setting the textbox to have a constant string value, taken from the aforementioned form label. However, to set the text in the textbox, the textbox must have focus. It can't have focus in design mode or print preview mode, and these are the only modes available for reports. Does anyone have a workaround in which I could set the contents of that textbox programmatically, so that users can save the report they have generated without having it display errors or incorrect text?
0
Comment
Question by:876
  • 6
  • 6
12 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9847040
In a standard module, under module tab paste:

Public TileStr As String
---------
In OnClick event of preview button, include:

TileStr =Me!txtTitle

And, in report OnOpen event include:

Me!txtReportTitle=TileStr

Mike
0
 
LVL 1

Author Comment

by:876
ID: 9847086
Hi, I gave that a try. Worked fine up until setting the contents of the textbox on the report (I tried the line of code above, tried substituting 'TileStr' with a constant string, and tried changing the '!' to a '.', none of which changed the outcome), where I got the following error:
Run-time error '2448':
You can't assign a value to this object.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9847113
Corrections...  and some questions for you:

Public TileStr As String                             'Q1:  Do you have in right place?
---------
In OnClick event of preview button, include:

TileStr =Nz(Me!txtTitle,"")                        'Q2: is txtTitle on your form and it is unbound?
MsgBox TileStr & "   <-- string read from the form; remove after a test"
---------
And, in report OnOpen event include:

Me!txtReportTitle=TileStr                     'Q3: is txtReportTitle on your report and it is unbound?
MsgBox TileStr & "   <-- string in report; remove after a test"

Mike
0
 
LVL 1

Author Comment

by:876
ID: 9847168
Re: Q1: It's sitting by itself in a module. I got no runtime errors upon assigning its value from the form.
Re: Q2: txtTitle is actually a label named lblDisplay. It is unbound. I set the contents programmatically based on selections made in other fields.
Re: Q3: txtReportTitle is called txtSubHead. Its ControlSource is as follows:
=Forms!frmProcessRelationships!lblDisplay.Caption
..as it is linked to the value in the label on the form.

I have changed my code as you suggested. I now get the following error:
438: Object doesn't support this property or method
on this line:
TileStr = Nz(Me!lblDisplay, "").

I get the same error on the next line (printing TileStr in a message box), so there must be a problem with accessing that member of that module for some reason...
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9847301
corrections...

In OnClick event of preview button, include:

TileStr =Me!lblDisplay.caption & ""                    
MsgBox TileStr & "   <-- string read from the form; remove after a test"
---------
And, in report OnOpen event include:

Me!txtReportTitle =TileStr    
' remove =Forms!frmProcessRelationships!lblDisplay.Caption  from its control source

Mike
0
 
LVL 1

Author Comment

by:876
ID: 9847919
I made the changes you recommended, including unbinding the textbox. When running the following line from the report:
Me!txtSubHead = TileStr
I get the following error:
Run-time error '2448':
You can't assign a value to this object.
I'm not sure if this is to do with it not having focus, or some other problem...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9847932
in

Me!txtSubHead = TileStr

I take that txtSubHead is a text box.  If it is a lable (we need to call it lblSubHead on the report and in the code) like

Me!lblSubHead.caption = TileStr
0
 
LVL 1

Author Comment

by:876
ID: 9847970
txtSubHead is indeed a textbox. I don't think I'd be able to bind it to my form if it was a label (but I'm not 100% sure about that).
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9848113
The control source of txtSubHead needs to be empty, is it?
0
 
LVL 1

Author Comment

by:876
ID: 9848129
It is empty.
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 160 total points
ID: 9848144
Try this

In Report OnOpen Event: (also try this code in OnFormat of report header)

Dim temp
temp=Nz(TileStr,"is null")
temp=iif(temp="","is empty",temp)
Msgbox temp                        '<-- to check to see if the variable is recognized
Me!txtSubHead ="ABCD"        '<-- to check to see if ABCD appears in txtSubHead


---------
In OnClick event of preview button, include:

TileStr =Me!lblDisplay.caption & ""                    
MsgBox TileStr & "   <-- string read from the form; remove after a test"

Let me know about two MsgBoxes and ABCD on the report.

Mike
0
 
LVL 1

Author Comment

by:876
ID: 9848199
Tried this in the Open event:
temp contained "is empty" when it came up in the msgbox
Me!txtSubHead ="ABCD" threw exception 2448 as documented earlier.

Tried it in the OnFormat event:
temp contained "is empty" when it came up in the msgbox
No unhandled exceptions; "ABCD" appears as the subheading!!

In preview button's Click event:
TileStr was successfully set to the value of the label.

I don't want to change the subheading if the report has been loaded without the use of the form. I'll put a flag in that global module to state whether the form loaded the report...

Thanks very much for your help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now