OnChange Event

  • Thread starter Thread starter peterm
  • Start date Start date
P

peterm

I'm running Access 2003 on XP. I have a form with a text
field on it. In the text fields OnChange event I have
the following code:
If me.textfield > "" then
me.addbutton.enabled = true
else
me.addbutton.enabled = false
endif

The problem I'm having is that it doesn't work as
expected; the OnChange event is firing for every
character that is entered into the text field but the
debugger says the value is null until focus moves from
the text field to any other control on the form. This
defeats the purpose. Why is the text fields value null
until the text box looses focus? I want the button
enabled upon the first character entered into the text
field. Thanks for your help!
 
Use the Text property of the control:

Dim bEnabled As Boolean

If Len(Me.textfield.Text) > 0 Then
bEnabled = True
End If

With Me.addbutton
If .Enabled <> bEnabled Then
.Enabled = bEnabled
End If
End With
 
You may be happier using the textbox's Text property, instead of its
(default) Value property, like this:

If me.textfield.text >"" then


HTH
- Turtle
 
Not sure how you're getting the info that '..the debugger says the value is
null..' In truth though, it's better to use

if trim(me.textfield) <> "" then
button.enabled = true
else
button.enabled = false
endif

or

if len(me.textfield) = 0 then
button.enabled = false
else
button.enabled = true
endif

HTH
Martin
 
As you have been advised by others, you should probably use the Text
property for what you are trying to do rather than the default Value
property.

Here is why (from Help):
"The Text property may be different than the Value property for a text box
control. The Text property is the current contents of the control. The Value
property is the saved value of the text box control. The Text property is
always current while the control has the focus."

That's why the Value property reads as Null in a new record until the
control loses focus: the contents of the control haven't been saved yet, so
it has no Value (but it does have Text as soon as you type the 1st
character).

HTH,
 
As you have been advised by others, you should probably use the Text
property for what you are trying to do rather than the default Value
property.

Here is why (from Help):
"The Text property may be different than the Value property for a text box
control. The Text property is the current contents of the control. The Value
property is the saved value of the text box control. The Text property is
always current while the control has the focus."

That's why the Value property reads as Null in a new record until the
control loses focus: the contents of the control haven't been saved yet, so
it has no Value (but it does have Text as soon as you type the 1st
character).

HTH,
 
Try the TextBoxControl_Dirty Event instead. This will fire only once on the
first character. You probably don't even need to check the length of the
Text of the Control.
 
Is this an event that's been added since A2K?
I'm familiar with Dirty events for forms, but not for controls...

- Turtle
 
My memory is that it was new in Access 2002.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top