InStr Function

J

JC

I'm using Access 2002 with SP2.

The InStr function is acting strangely. I'm trying to use
the On Change property of a text box to set the Enable
property of a command button to true or false depending on
whether there is or isn't a space in the text box string.

The On Change property certainly works. However, the
InStr function seems to get confused after the first time
it's called. Is this a known bug? Is there a workaround -
such as a way to reset the InStr function?

jc
 
L

Larry Linson

"On Change" fires with every keystroke typed into the Control -- could that
be contributing to your problem? Only very specific kinds of event handling
are suitable for "every keystroke" use.

Larry Linson
Microsoft Access MVP
 
J

J.C.

Larry -
The On Change event works fine. Every keystroke fires
it. The problem is that within the code for On Change, I
test the contents of the Text Box control using the InStr
Function. However the function fails to properly test the
contents.

For example,

If InStr(TextBoxName, " ") = 0 then
ControlButtonName.Enabled = False
Else
ControlButtonName.Enabled = True
End If

doesn't reflect the addition or removal of a space.

jc
 
J

J.C.

Doug -
Take a look at how I answered the other response. Seems
like this should work, but it doesn't. Hmmmm.
jc
 
K

Ken Snell

If you're going to use the OnChange event, then use the Text property, not
the Value property (which is the default property for the control when you
don't specify one):

If InStr(TextBoxName.Text, " ") = 0 then
ControlButtonName.Enabled = False
Else
ControlButtonName.Enabled = True
End If
 
J

J.C.

Works like a charm! Thanks for the hint.

jc
-----Original Message-----
If you're going to use the OnChange event, then use the Text property, not
the Value property (which is the default property for the control when you
don't specify one):

If InStr(TextBoxName.Text, " ") = 0 then
ControlButtonName.Enabled = False
Else
ControlButtonName.Enabled = True
End If
--
Ken Snell
<MS ACCESS MVP>




.
 
G

Gary Schuldt

For those of us benighted ones (maybe only me), why does .Text work and not
..Value? You imply that it has something to do with the OnChange event.
Maybe if I understand the reason I won't be asking the same question 6 mos
from now!

Thanks.

Gary
 
J

John Spencer (MVP)

A control bound to a field has several properties that apply here.

.OldValue -- the value of the field that is stored in the current record
.Value -- the current value of the field as it is stored in memory
.text -- the value that is being displayed/changed in the currently selected
control

Note that all these "values" could be the same or they could all be different
depending on the state of teh current form (dirty, not dirty) and the state of
current control (data being changed, not being changed).

In reference to ONE control on ONE form:
Open a record, set focus to a control - all three values are the same
Edit the contents of the control, but don't change the focus, OldValue and Value
remain the same, Text changes
Exit the control, Text is no longer available for that control, OldValue and
Value are different. Value is now equal to what Text was.
Save the record. OldValue and Value are the same.

Hope that brief and incomplete summary will help your understanding.
 
D

Dev Ashish

why does .Text work and not
.Value

Value is the 'committed value' of the textbox, something that doesn't
happen until the OnChange event fully completes. Text is the 'uncommitted
value', or the value in-transition, which may or may not be committed. From
most other events, 'Value' is what you would use.

-- Dev
 
G

Gary Schuldt

Thanks! Helps out a lot. In fact, when combined with the fact that .Value
is the default, it might explain the behavioral mysteries of some of my
controls.

Gary
 

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