Solved

Programmatically set text in textbox on Access Report

Posted on 2003-11-30
12
1,999 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 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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