Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running AfterUpdate control event when key Enter has its own behaviour

Status
Not open for further replies.

Bresart

Programmer
Feb 14, 2007
314
ES
Hi, in a form there’s a combobox in which AfterUpdate event the code filters the records which name field matchs with the value introduced in the combobox.

The combobox Limit to list property is set to no. Then, if the value is selected from the list there’s no problem, but if the value is written, the Enter key must be pressed for the the filter be applied (run the AfterUpdate event of the combobox), and as result of that the focus goes from the first control of the form to the second one when the filter is applied because in each control in the form the KeyPress procedure displaces the focus to the next control if the pressed key is Enter.

Which is the way of running the AfterUpdate event of the combobox when its value is keyed preventing the running of the KeyPress event of the form first control? When the AfterUpdate event procedure of the combobox is preceded by the line

Msgbox “”


the KeyPress event of the form first control doesn’t enter.

Thanks for any help given.
 
Is the problem that the control loses focus? If so, why not set focus again? For example:

Code:
Private Sub txtB_AfterUpdate()
    Me.Filter = "txtA Like 'Abc*'"
    Me.FilterOn = True
    Me.txtB.SetFocus
End Sub


 
Thanks Remou.

No, the focus must go and stay in the first control of the form.

The focus goes to the first control once applied the filter because the Form_Current event procedure of the form is

DoCmd.GoToControl "firstControlName"


but having pressed the Enter key for updating the combobox control, once that in the form first control the KeyPress event procedure of the first control runs, it displaces the focus to the second control (because in each control in the form the KeyPress procedure displaces the focus to the next control if the pressed key is Enter).

 
Are you saying that you have a KeyPress event for the form? If so, check the name of the ActiveControl and / or PreviousControl and do something other than move to the next control. I am not quite sure why you would use a keypress event for enter when the default behaviour is to move to the next control. If you do not have a keypress event, the example I showed should help. If you do not wish to prevent moving to the second control, please say what you wish to do.
 
Remou, no, the KeyPress event is the one of each control of the form.

The code must prevent moving to the second control only when the filter is applied, in the rest of cases it must displace the focus from the first to the second control of the form.

I have tried:

Private Sub Combo_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
enterKeyPress = True
End If
End Sub

Private Sub Combo_AfterUpdate()
If enterKeyPress = True Then
enterKeyPress = False
enterAfterUpd = True
Else
enterAfterUpd = False
End If
End Sub

Private Sub consistCombo_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Or KeyAscii = 9 Then
If enterAfterUpd = False Then
DoCmd.GoToControl "grupoCultivoCombo"
Else
KeyAscii = 0
eKPandAfterUpd = False
End If
End If
End Sub



but though enterKeyPress is True when Combo_KeyPress ends if Enter key is pressed, the line

If enterKeyPress = True Then


in the beginning of the next event, Combo_AfterUpdate, doesn't enter because then surprisingly enterKeyPress is False.


Thanks.
 
I think you need another event. How about:

Code:
Private Sub consistCombo_KeyDown(KeyCode As Integer, Shift As Integer)
'If the combo is being changed ...
If Me.consistCombo.OldValue <> Me.consistCombo.Text And KeyCode = 13 Then
    'Cancel return
    KeyCode = 0
End If
End Sub
 
Thanks, Remou.

It doesn't work for this case.


Anyway, the correct code i have is (correcting my previous post):

Private Sub Combo_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
enterKeyPress = True
End If
End Sub

Private Sub Combo_AfterUpdate()
If enterKeyPress = True Then
enterKeyPress = False
enterAfterUpd = True
Else
enterAfterUpd = False
End If
End Sub

Private Sub firstControl_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Or KeyAscii = 9 Then
If enterAfterUpd = False Then
DoCmd.GoToControl "secondControl"
Else
KeyAscii = 0
enterAfterUpd = False
End If
End If
End Sub





Replacing the line

If enterAfterUpd = False Then


with the line

If Me.Combo_OldValue = Me.Combo.Value Then


the equality is always true, the two equality members are never different, so the code always displaces the focus to the second control. Replacing Me.Combo.Value with Me.Combo.Text, the Text property needs the focus is on the control Combo.
 
With the booleans it has worked by moving the code of the first procedure, from Combo_KeyPress to for Combo_KeyDown.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top