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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Key Down Function 1

Status
Not open for further replies.

agfon

Programmer
Mar 25, 2005
38
0
0
US
I have a series of textboxes designed to allow the user to input in query criteria. I would like to set them up to utilize on "On Key Down" function to start the query when the Enter key is pressed. This would be instead of setting the "On Key Down" sub for each field.

Has anyone ever done this before? I would appreciate any input.

-agfon
 
Are you expecting that they be pressing the enter key after entering data in each field or after they have entered data in every field? I've found that many users are not accustomed to using the TAB key to move between fields. They end up pressing enter.
 
These users are used to hitting 'Enter' to exit the field and move the focus to the Cmd_button that fires the query. I've expanded their selection of fields; however, I would still like the set-up to be similar.

-agfon
 
Are you refering to creating a function that can be used in each fields on key down event.

If you are, write the function into the form module, it cannot be a sub.
Select all the fields that you want to use the function
Add the function to the event you want to use the function

=doSomething()
*include '=' sign and () at end of function

In form's vba code, set keypreview on so keys are handled by the form first. If key is enter, the set isEnter to true so when doSomething is called it can test if the key down is the enter key. Remember you can't just paste the code, after pasting you have to set the form's keydown event to the code.

Code:
option explicit
dim isEnter as boolean
Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'test if key hit is enter
    isEnter = (keyCode = 13) '13 - Enter KEy
End Sub

private sub doSomething()
     if( isEnter ) then
     end if
end sub
 
Good code stix4t2, but you were explicit about the procedure being a function, weren't you....?(LOL)
 
Your right, oops

private function doSomething()
if( isEnter ) then
end if
end sub
 
The easy way, is to set the default propety of the command button you're using to yes ;-)

But this is more form stuff - forum702

Roy-Vidar
 
stix4t2,

Thank you very much for the code. It was exactly what I was looking for.

-Fred
 
Stix42,
I learned something with the KeyPreview. This gave me an idea about trapping a single event for multiple controls by incorporating this with the active control property. Here is a dumb example, but I think if you had a form with lots of textboxes this could come in handy if you want to do something different depending on the Name, Value, or Tag of the control.

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'test if key hit is enter
    If KeyCode = 13 Then doSomething
End Sub

Private Sub doSomething()
    Dim myTextBox As Access.TextBox
    Dim myLabel As Access.Label
    If ActiveControl.ControlType = acTextBox Then
      Set myTextBox = ActiveControl
      With myTextBox
      MsgBox .Name
          If Not .BackColor = vbRed Then
            .BackColor = vbRed
            .SpecialEffect = Abs(.SpecialEffect - 1)
         Else
            .BorderStyle = Abs(.BorderStyle - 1)
            .BackColor = vbYellow
         End If
      End With
    End If
    
End Sub
 
disregard the Access.label. I do not think a label can ever by the active control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top