Error when getting text value from textbox

R

Robin9876

Below is the error that is displaued when getting the text value for a
textbox which the previous line sets the focus to this textbox.

Run-time error '2185'

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


Is there a particular method of accessing the text value from a change
event of another control on the same form?
 
P

Pieter Wijnen

MyTextBox.value
The Text Property is not the same as Visual Basic's Text Property, Value is


Pieter
 
G

Guest

You can reference the Text property of a control, but only if it has the
focus. However, in Access VBA it is seldom necessary to reference either the
Text or the Value properties. The default property of a control is the Value
property, so

Me.SomeTextBox.Value
Me.SomeTextBox

will return the same results. So to will
Me.SomeTextBox.Text
If Me.SomeTextBox has the focus.
 
P

Pieter Wijnen

It's good practise to do so anyway, both because it documents your code &
it's easier to port code to for instance vbscript
also the code runs faster as Access doesn't have to do the extra search for
the default property
on the same note the (.) notation executes faster than (!).
It also comes as a great surprise to most people that using the len function
to check for an empty string is radically faster than checking against an
empty string

If s = "" Then ' Slowest, multiple copy & assignment operations
if s = VBA.vbNullString ' Somewhat faster, Doesn't have to alloc new memory
for the Empty String at least
If VBA.Len(s) = 0 ' Fastest, Slight overhead from the function call, but no
new memory allocation (The length is stored in the zeroeth byte of the
string)

Pieter
 
G

Guest

I agree with and am aware of all your points except the use of the Value
property. To verify you assertion it is faster to use it, I will have to do
some timing tests.
Now, if you want to think about performance, using the With End With
construct improves performance as well.
 

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