E
Edward Reid
I want pressing Return in a text box to activate a nearby command. It
sounds so simple. I figured out that I have to use the KeyPress event.
That was easy, as was testing for a Return, and from there I called the
Click sub for the related command button. Then the fun began.
First, the Click sub was getting the .Value of the field I came from,
which hadn't been stored since the field was still active. So I tried
storing .Text into .Value before calling the Click sub. (It isn't
reasonable for the Click procedure to look at the .Text property. For
one thing, the real Click sub uses several text boxes to build its
query. Also, that would require moving the focus around to each text
box, or perhaps figuring out which one has the focus and using .Text
from that box and .Value for the others. Also, control validation rules
would be skipped.)
OK, so .Value = .Text mostly works. But that also appears to skip the
control validation. In fact, it looks like the unchecked string gets
permanently stored into .Value. For example, the field is formatted
"general date". If I type "3/21" (being in the US) and tab out, it's
changed to "03/21/2006". But if I press Return instead of Tab, I
execute the .Value = .Text code, and the field shows as "3/21" no
matter how often I move in and out, until I change it again.
I also had to add the check for empty string in addition to null,
though possibly I should have had that in the code anyway.
BTW, all of the fields in question are unbound. In fact, all the fields
on the form are unbound -- it's a sort of portal inquiry form.
All I want is to validate the current contents (.Text) and run some
code which ends up with a DoCmd.OpenForm, where the selector for the
new form is based in part on the content of the current field. Is there
an easier way? Should I do MyCmd.SetFocus and send it a click key?
Would that validate MyDateBox? Something else? I've done enough
experiments that I want to ask before continuing to grope around in the
dark.
At the moment my code looks very much like this:
Private Sub MyDateBox_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
MyDateBox.Value = MyDateBox.Text
MyCmd_Click
End If
End Sub
Private Sub MyCmd_Click()
If IsNull(MyDateBox) Or Len(MyDateBox) = 0 Then
MsgBox "Enter date"
Else
' build a LinkCriteria string involving MyDateBox
' call DoCmd.OpenForm
End If
End Sub
The above are repeated for 16 text boxes and 8 command buttons.
Edward
sounds so simple. I figured out that I have to use the KeyPress event.
That was easy, as was testing for a Return, and from there I called the
Click sub for the related command button. Then the fun began.
First, the Click sub was getting the .Value of the field I came from,
which hadn't been stored since the field was still active. So I tried
storing .Text into .Value before calling the Click sub. (It isn't
reasonable for the Click procedure to look at the .Text property. For
one thing, the real Click sub uses several text boxes to build its
query. Also, that would require moving the focus around to each text
box, or perhaps figuring out which one has the focus and using .Text
from that box and .Value for the others. Also, control validation rules
would be skipped.)
OK, so .Value = .Text mostly works. But that also appears to skip the
control validation. In fact, it looks like the unchecked string gets
permanently stored into .Value. For example, the field is formatted
"general date". If I type "3/21" (being in the US) and tab out, it's
changed to "03/21/2006". But if I press Return instead of Tab, I
execute the .Value = .Text code, and the field shows as "3/21" no
matter how often I move in and out, until I change it again.
I also had to add the check for empty string in addition to null,
though possibly I should have had that in the code anyway.
BTW, all of the fields in question are unbound. In fact, all the fields
on the form are unbound -- it's a sort of portal inquiry form.
All I want is to validate the current contents (.Text) and run some
code which ends up with a DoCmd.OpenForm, where the selector for the
new form is based in part on the content of the current field. Is there
an easier way? Should I do MyCmd.SetFocus and send it a click key?
Would that validate MyDateBox? Something else? I've done enough
experiments that I want to ask before continuing to grope around in the
dark.
At the moment my code looks very much like this:
Private Sub MyDateBox_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
MyDateBox.Value = MyDateBox.Text
MyCmd_Click
End If
End Sub
Private Sub MyCmd_Click()
If IsNull(MyDateBox) Or Len(MyDateBox) = 0 Then
MsgBox "Enter date"
Else
' build a LinkCriteria string involving MyDateBox
' call DoCmd.OpenForm
End If
End Sub
The above are repeated for 16 text boxes and 8 command buttons.
Edward