Get value from textbox when it doesnt have the focus

M

moondaddy

I have a form where I need to get the value from a number of textboxes in a
single event. If the textbox has the focus I can use code like this: var =
mytextbox.text, but if it doesn't have the focus (which all but one can't
have the focus) I'll get an err msg saying you cant reference a control if
it doesn't have the focus. So now if I use code like this: var = mytextbox,
I sometimes get different results and I haven't figured out why/when they
happen that way, but the 2 different results are these:
1) I get the value returned from the text in the textbox (which is what I
expect)
or
2) mytextbox returns a value of null even when there's text in it, in which
case var = mytextbox returns an err because I'm trying to make var equal to
null.

This behavior is inconsistent and very frustrating. in vb6 and vb.net I can
get the value from mytextbox.text even when it doesn't have the focus.

Can anyone please advise on how to consistently get the value from
mytextbox. Note, for reasons, I cant use events like mytextbox.onchange. I
need to capture all the values from all the textboxes in a single event so
most of them wont have the focus.

Thanks.
 
M

Marshall Barton

moondaddy said:
I have a form where I need to get the value from a number of textboxes in a
single event. If the textbox has the focus I can use code like this: var =
mytextbox.text, but if it doesn't have the focus (which all but one can't
have the focus) I'll get an err msg saying you cant reference a control if
it doesn't have the focus. So now if I use code like this: var = mytextbox,
I sometimes get different results and I haven't figured out why/when they
happen that way, but the 2 different results are these:
1) I get the value returned from the text in the textbox (which is what I
expect)
or
2) mytextbox returns a value of null even when there's text in it, in which
case var = mytextbox returns an err because I'm trying to make var equal to
null.

This behavior is inconsistent and very frustrating. in vb6 and vb.net I can
get the value from mytextbox.text even when it doesn't have the focus.

Can anyone please advise on how to consistently get the value from
mytextbox. Note, for reasons, I cant use events like mytextbox.onchange. I
need to capture all the values from all the textboxes in a single event so
most of them wont have the focus.


In Access VBA, you should use the Value property instead of
the Text property. (The Text property is only used to get
the control's uncomitted value as it is being entered.)

If you place your code in the control's AfterUpdate event,
the control's value will be stable. The Change event fires
for every keystroke, before the value has been determined.

It may be helpful for you to be aware of the NZ function.
 
M

moondaddy

Thanks. I have used the value property in the past on this form, but it was
a long time ago and I forget the reason why I stopped using it. I'm well
aware of using the value property on the after update event, but like I said
in the post below, I can't use the afterupdate event. I have to get the
value from a method which collects all the values on one single call and
therefore, the control wont have the focus, and therefore I get the error
"cant reference a control when it doesn't have the focus". So we're still
back to where I started, how do I get the value from a textbox (or any
control) when it doesn't have the value?
 
M

Marshall Barton

moondaddy said:
Thanks. I have used the value property in the past on this form, but it was
a long time ago and I forget the reason why I stopped using it. I'm well
aware of using the value property on the after update event, but like I said
in the post below, I can't use the afterupdate event. I have to get the
value from a method which collects all the values on one single call and
therefore, the control wont have the focus, and therefore I get the error
"cant reference a control when it doesn't have the focus". So we're still
back to where I started, how do I get the value from a textbox (or any
control) when it doesn't have the value?


I think you're confusing several different situations. If
the control does not have the focus, then you do not want
its Text property, you want its Value property.

You can get the Value of any text box in the AfterUpdate
event of any control. Please try it, maybe then you'll get
a clearer view of what's happening or be able to explain
what's wrong in a way that I can follow.
 
M

moondaddy

Thanks Marsh, but you keep missing an important thing I've been mentioning
in every posting. Because of a certain business process, I cant collect the
value in the afterupdate event. I need to collect the values of all the
controls in one single event (a button click event). And therefore, all the
controls will not have the focus. This would be easy if I could do it from
the afterupdate event because of course the control would still have the
focus in the afterupdate event. If I try to get the controls value from the
button click event, I get this error:

"You can't reference a property or method for a control unless the control
has the focus."

So once again, it there a way to get the value of a textbox (or any control)
when it doesn't have the focus?
 
M

Marshall Barton

moondaddy said:
Thanks Marsh, but you keep missing an important thing I've been mentioning
in every posting. Because of a certain business process, I cant collect the
value in the afterupdate event. I need to collect the values of all the
controls in one single event (a button click event). And therefore, all the
controls will not have the focus. This would be easy if I could do it from
the afterupdate event because of course the control would still have the
focus in the afterupdate event. If I try to get the controls value from the
button click event, I get this error:

"You can't reference a property or method for a control unless the control
has the focus."

So once again, it there a way to get the value of a textbox (or any control)
when it doesn't have the focus?


We seem to be talking past each other.

I can only repeat that you can get the Value of any control
from most any event (including a button's Click event)
without worrying about the focus. The focus is only
relevant to the uncommon situations where you need to use
the Text and a very few other special purpose properties.
(If you're stuck in a VB6 mindset, then shift gears and
start using Value instead of Text.)

For example, a button's Click event could calculate and
display the sum of two other text boxes by using:

Me.textbox3.Value = Me.textbox1.Value + Me.textbox2.Value

or because of default object and property, simply:

textbox3 = textbox1 + textbox2

To avoid potential ambiguity between various name spaces, I
prefer to use:

Me.textbox3= Me.textbox1+ Me.textbox2

but, in most situations, they all produce the same result.

If none of this applies to whatever you're doing, please
post a Copy/Paste of your code and an explanation of what is
different from what I've been reading into your explanation
of the problem.
 
M

moondaddy

Sorry for the delay in responding, I don't get to work on this project very
often.

Here's some information from the watch window for you:

Watch : + : Me.txtLegal : "" : Object/Textbox :
Form_frmSearchCrit.BuildCriteria
and
Watch : : Me.txtLegal.Value : "" : Variant/String :
Form_frmSearchCrit.BuildCriteria
and
Watch : : Me.txtLegal.Text : "like *hunter*" : String :
Form_frmSearchCrit.BuildCriteria


I entered "like *hunter*" into the textbox, checked another checkbox, and
then clicked on the button that runs this code. Above you can see the value
of the properties for the textbox. The only time I can consistently get the
correct value is when I use the text property, but because the control
doesn't have the focus, I cant use the text property.

Why is it that it's value is ""?

This is really getting frustrating.
 
M

Mingqing Cheng [MSFT]

Hi moondaddy,

I would like to follow up this thread helping your resolving this issue:)

Would you please send me a sample describing your issue? Detailed
information, I believe, will make us closer to the resolution. I could be
reached at (e-mail address removed) (remove online before sending:)

Thank you for your patience and cooperation. If you have any questions or
concerns, don't hesitate to let me know. We are here to be of assistance!


Sincerely yours,

Mingqing Cheng
Microsoft Developer Community Support
 
M

Marshall Barton

moondaddy said:
Sorry for the delay in responding, I don't get to work on this project very
often.

Here's some information from the watch window for you:

Watch : + : Me.txtLegal : "" : Object/Textbox :
Form_frmSearchCrit.BuildCriteria
and
Watch : : Me.txtLegal.Value : "" : Variant/String :
Form_frmSearchCrit.BuildCriteria
and
Watch : : Me.txtLegal.Text : "like *hunter*" : String :
Form_frmSearchCrit.BuildCriteria


I entered "like *hunter*" into the textbox, checked another checkbox, and
then clicked on the button that runs this code. Above you can see the value
of the properties for the textbox. The only time I can consistently get the
correct value is when I use the text property, but because the control
doesn't have the focus, I cant use the text property.

Why is it that it's value is ""?

This is really getting frustrating.


I've been out of touch for the last two weeks. Anyway, I
can see where this would be driving you nuts. That watch
info, in that context, makes no sense to me.

The only thing I can think of is that you have some kind of
corruption in your MDB file. I hope that Cheng can help
straighten this out for you. If not, maybe something at
Tony's FAQ page:
http://www.granite.ab.ca/access/corruptmdbs.htm
will provide some insight.
 

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

Top