Solved

Programmatically set text in textbox on Access Report

Posted on 2003-11-30
12
2,052 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 34

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 34

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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 34

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
 
LVL 34

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 34

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 34

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

626 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