Solved

Programmatically set text in textbox on Access Report

Posted on 2003-11-30
12
1,947 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

920 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