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

Will a Form's, not control's, KeyDown event slow my app?

Status
Not open for further replies.

dweis

IS-IT--Management
Apr 17, 2001
48
US
I have a subform (details) in a main form (header). The detail form is in datasheet view. My users want the ability to input data in the detail form & when they press Enter from any field it should add a new record. I have the code that will trap the Enter key and go to a new record in the activeobject. My code is in the Keydown event of the Detail form and is a case statement. Case 1 the F3 is pressed & I need to open a new form. Case 2 the Enter key is pressed & I need to add a new record.

Will testing for keys in the keydown of a form considerably slow the app since every keystroke is tested? Is there an alternate way without a button (I copy several fields from the previous line item when the Enter key is pressed) Thanks in advance for your help. Access 2002 in multiuser FE/BE environment.
 
Yes, it will slow the app down, but not appreciably. The system is already generating events for every keystroke. The slow down comes from additionally exposing that event to your application and to execute the code in the event handler. How much it slows down your app depends on how efficiently you code the event handler.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Great, I didn't want to lose the functionality. Code is below. Let me know if you see any problems. Thanks for your help.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo err:
Select Case KeyCode
Case vbKeyF3
DoCmd.OpenForm "addl_chg_fr_leg"
GoTo ProcedureExit
Case vbKeyReturn
Dim a, b, c
If [Forms]![header_De]![move_type] = "Road" Then

a = Me.Container
b = Me.del
c = Me.leg

DoCmd.GoToRecord acActiveDataObject, , acNewRec
Me.Container = a
Me.pu = b
Me.leg = c + 1


GoTo ProcedureExit
End If
If [Forms]![header_De]![move_type] = "Local" Then
Dim d, e
a = Me.pu
b = Me.del
c = Me.pu_date
d = Me.del_date
e = 1
DoCmd.GoToRecord acActiveDataObject, , acNewRec
Me.pu = a
Me.del = b
Me.leg = e
Me.pu_date = c
Me.del_date = d
GoTo ProcedureExit
End If
End Select
GoTo ProcedureExit

err:
Dim result
result = MsgBox("There's an error on this leg, please check. You can't go to a new record at this time.", vbOKOnly)
ProcedureExit:
End Sub
 
There are a couple of things that I would do differently. First of all, I don't like this much code an event handler so I would move most of it out. In the code itself, you have several unnecessary GoTo statements, and you are not consistent in the declaration of your variables, and you also declaring them as variants which take more time and space than is necessary. I can't tell from the names what types of controls these are, but none of these variables may even be necessary. You are not closing out the error handler with a Resume statement, which is the preferred method, as it resets the Error objects, and you don't need the return value from your message box. The second If statement inside of vbKeyReturn case will not be necessary if "Road" and "Local" are the only two valid values for [move_type].
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

   ProcessKeyStroke KeyCode
   
End Sub

Private Sub ProcessKeyStroke(rInt_KeyStroke As Integer)

   On Error GoTo ProcessError
   
   Select Case rInt_KeyStroke
      Case vbKeyF3
         DoCmd.OpenForm "addl_chg_fr_leg"
      Case vbKeyReturn
         If [Forms]![header_De]![move_type] = "Road" Then
            Me.Container.Tag = Me.Container.Value
            Me.del.Tag = Me.del.Value
            Me.Leg.Tag = Me.Leg
            DoCmd.GoToRecord acActiveDataObject, , acNewRec
            Me.Container.Value = Me.Container.Tag
            Me.pu = Me.del.Tag
            Me.Leg = Val(Me.Leg.Tag) + 1
         Else
            If [Forms]![header_De]![move_type] = "Local" Then
               Me.pu.Tag = Me.pu.Value
               Me.del.Tag = Me.del.Value
               Me.pu_Date.Tag = Me.pu_Date
               Me.del_date.Tag = Me.del.Value
               DoCmd.GoToRecord acActiveDataObject, , acNewRec
               Me.pu.Value = Me.pu.Tag
               Me.del.Value = Me.del.Tag
               Me.Leg.Value = 1
               Me.pu_Date.Value = Me.pu_Date.Tag
               Me.del_date.Value = Me.del_date.Tag
            End If
         End If
   End Select
   
Exit_ProcessKeyStroke:
Exit Sub

ProcessError:
   
   MsgBox "There's an error on this leg, please check. You can't go to a new record at this time.", vbOKOnly
   Resume Exit_ProcessKeyStroke

End Sub
The final thing is that I would not use the KeyDown event to trigger this process. I would use the KeyUp event. The reason is that if the user holds down the key, sits on the return key a little too long, the KeyDown event and the KeyPress event will alternately continue to fire and this code may execute several times. The KeyUp event will only occur one time per keystroke.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Great, thanks for the tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top