Property ctl.Value not set when...

  • Thread starter Thread starter John Keith
  • Start date Start date
J

John Keith

Property ctl.Value not set when...Pressing Enter

The form I am working with is a search routine using unbound text boxes in
the header for the criteria. I have set up the form to detect keypresses:
Enter or Esc in the Form_Keydown routine and call the search routine that is
stored in a cmdFilter_Click (or the cmdReset_Click for the Esc key)

I think what is happening is that the KeyDown event is firing before the
search text has been registered into the .Value propery of the control.
Because, If I press Enter again after no search critera were recognized, the
routine then sees the .value just fine. Also, if I enter the value in the
search box and then use the mouse to click the cmdFilter button, the routine
works just fine.

So, Is there a way to force the entered text to update the control as the
Keydown routine fires? OR what is another way to make the enter key run the
search.
Perhaps using the form level keydown is too early and should be moved... to
where? The help files had suggested this to avoid having to code every
control with a set of key press dection routines.
 
John Keith said:
Property ctl.Value not set when...Pressing Enter

The form I am working with is a search routine using unbound text boxes in
the header for the criteria. I have set up the form to detect keypresses:
Enter or Esc in the Form_Keydown routine and call the search routine that
is
stored in a cmdFilter_Click (or the cmdReset_Click for the Esc key)

I think what is happening is that the KeyDown event is firing before the
search text has been registered into the .Value propery of the control.

That is correct. The control's Value property is updated when the focus
leaves the text box, but not before. In the control's KeyDown event, the
value has not yet been updated.

Normally, one would use the control's AfterUpdate event to trigger any
operation that is to be based on its value. In the AfterUdate event, by
definition the value has been updated.

If you want to filter *as you type*, keystroke by keystroke, then you can
use the Change event, which fires every time the text displayed in the
control changes.
 
John Keith said:
Perhaps using the form level keydown is too early and should be moved...
to
where? The help files had suggested this to avoid having to code every
control with a set of key press dection routines.

What I do when I want to code the same behavior for a set of controls is
define a function in the form, or in a standard module, and then set the
event properties of all those controls to the same function expression.
It's easy to set them all if you select the whole set of controls and bring
up their joint property sheet.

For example, I might set the AfterUpdate property of all the controls to:

=FilterThisForm()
 
Well, in most cases, you let the user edit the text box, and then they hit
enter, or tab.

At that point, you can/should use the after update event of the text box.

If you ARE doing key press processing, then you WILL have to use the .text
property.

I often setup a search form in which the ESC key clears the search text, and
re-sets the focus to the search text box.

However, for the search text box, I use the controls after update event
(unless you want the search to occur for each key, but I find that takes too
much bandwidth, and often the user just needs to type in a few characters
and THEN have the search occur...really no need to start firing off
searching code as the user types...let them finish, and when they are
done..they hit enter, or tab key..and you use your code in the after update
event of the control..(no need to keypress processing here).

So, yes..if you are using the key down, then you have to use the .text
property. However, for capture of enter key (or most users have been trained
to use tab these days), use the controls after update event. This not only
means you don't have to trap for enter key, or tab key, but users are free
to use either key...and your search/processing code will NOT run until the
user hits tab/enter....

I outline how searching can work here:

http://www.members.shaw.ca/AlbertKallal/Search/index.html

And, if you download my super easy word merge here:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

The above has a sample search form, and you see how I just simply use the
after update event of the unbound text box...
 
Thanks! knowing that the focus leaving the textbox causes the .value to
update...

Adding this line in the Form_KeyDown routine does the trick.
Me.Controls("cmdFilter").SetFocus
 
John said:
Property ctl.Value not set when...Pressing Enter

The form I am working with is a search routine using unbound text boxes in
the header for the criteria. I have set up the form to detect keypresses:
Enter or Esc in the Form_Keydown routine and call the search routine that is
stored in a cmdFilter_Click (or the cmdReset_Click for the Esc key)

I think what is happening is that the KeyDown event is firing before the
search text has been registered into the .Value propery of the control.
Because, If I press Enter again after no search critera were recognized, the
routine then sees the .value just fine. Also, if I enter the value in the
search box and then use the mouse to click the cmdFilter button, the routine
works just fine.

So, Is there a way to force the entered text to update the control as the
Keydown routine fires? OR what is another way to make the enter key run the
search.
Perhaps using the form level keydown is too early and should be moved... to
where? The help files had suggested this to avoid having to code every
control with a set of key press dection routines.


The text you entered is not committed to the Value until the
AfterUpdate event.

Unless you are calling your search routine on every
keystroke, there is no need to look at the value. And
there is probably no need to examine keystrokes, especially
for the purpose of having the enter key call the search
routine. The AfterUpdate event can be used for that without
any fuss.

Even if you are calling the search routine on each
keystroke, the Change event is a lot easier than using
keydown.
 
Back
Top