Error with Textbox

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.
 
A

Allen Browne

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.
 
J

John W. Vinson

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Dim an unbound textbox? 6
Referencing a view for Recordsource 3
GoToControl 4
Error 2185 2
Refresh unbound textbox 5
dlookup based off of mutliple text box values 2
Changing the focus 2
Form/SubForm Problem 3

Top