Henrik Erlandsson
Henrik Erlandsson

Reputation: 3831

Can't change Text of TextBox in Report_Open

All I want to do is set a textbox or label's text to something dynamic when the report is opened with a click of a button in another form. I've solved everything except actually changing the text.

This code gives run-time error 2478 on SetFocus:

Me.tFilial.SetFocus
Me.tFilial.Text = filialen

Without SetFocus I get a run-time error saying the text can't be changed without switching control to the control in question.

What is allowed where is always in question in Access, it seems. How do I solve this? Can I set the value on the buttonclick in the other form with

Reports![rptPressSchema]![tFilial].text="Hello"?

I would be happy to use a label instead, if that solves it. But the bottom line is I can try to do this every which way but I thought I'd ask you for advice as to best practice, as this must be a very common task indeed.

Upvotes: 3

Views: 10733

Answers (2)

ZY Huang
ZY Huang

Reputation: 63

spent a lot of time for searching and trying. Finally figure the things out... To dynamically set the TextBox content, it is convenient to use tbTest.Value = "hello"

but the trick is if you are using this On Open, it will be in trouble...

Run-time error '2448' You can't assign a value to this object.

So you need set the value On Load

Private Sub Report_Load()
Me.tbTest.Value = "hello"
End Sub

Private Sub Report_Open(Cancel As Integer)
'Me.tbTest.Value = "hello"
End Sub

I see nowhere for any explanation of this, my guess is for Open event, the object is still not initiated (document for explaining Load and Open event)...

Upvotes: 1

mwolfe02
mwolfe02

Reputation: 24207

From the Access help:

While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.

Basically, the .Text property serves no purpose in a Report because individual controls cannot receive the focus. However, as @Remou stated in his comment, you can simply replace .Text with .Value and your code should work fine (no need to set focus when updating the value).

Upvotes: 3

Related Questions