Error with Textbox

  • Thread starter Thread starter Tim McGavin
  • Start date Start date
T

Tim McGavin

I am updating a textbox like this...

Text4.text = "hello"

When I try to run the code I get this error...
"you can't reference a property or method for a control unless the control
has the focus"

I can solve the problem by altering my code like this...

Text4.SetFocus
Text4.text = "hello"

.....But that doesn't seem correct. Is that right? Are you telling me that
you have to give each textbox focus if you want to update it?

Maybe that's right, but I just wanted to make sure I wasn't missing
something.
 
Omit the .Text property, i.e.:
Text4 = "hello"
or perhaps:
Me.Text4 = "hello"

Unlike pure VB, Access is a data-centric program, so text boxes have a
Value, which is the default property. The above is therefore equivalent to:
Me.Text4.Value = "hello"

The Text property applies only while the control has focus, and represents
what's in the control. It may not be the value - in fact it may not be
possible for it to be the value. For example, if you are typing a date, the
Text property may contain:
5/7/
but that could never become the Value, as it is not a valid date. Similarly,
32/13/2007 could be the Text property, but could not be the date. Or 999
could be the Text, but could not be the value if the text box is bound to a
Number field of size Byte.
 
I can solve the problem by altering my code like this...

Text4.SetFocus
Text4.text = "hello"

....But that doesn't seem correct. Is that right? Are you telling me that
you have to give each textbox focus if you want to update it?

The Text property exists only when the control has the focus... but the (more
important usually, and the default) Value property is available anytime.

Me!Text4 = "Hello"

works fine.

John W. Vinson [MVP]
 
Back
Top