Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How To Put Code In A Sub? (a2k (9.0.6926) SP-3 Jet 4.0 SP-7)

    I have the following code on a form and it works OK:

    Private Sub strCollectorCode_KeyDown(KeyCode As Integer, Shift As Integer)

    If KeyCode = (vbKeyDown And Shift = acAltMask) Or _
    KeyCode = vbKeyF4 Or KeyCode = vbKeyS Then
    Call lngID_DblClick(False)
    Else
    If KeyCode = vbKeyEscape Then
    DoCmd.Close acForm, Me.Name, acSaveNo
    End If
    End If

    End Sub

    I have several other fields that can use the same code.

    How can I take the above code and put it into a Sub that the following controls can call.

    Private Sub strDeposit_KeyDown(KeyCode As Integer, Shift As Integer)

    End Sub

    Private Sub dtmRevDate_KeyDown(KeyCode As Integer, Shift As Integer)

    End Sub

    I don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How To Put Code In A Sub? (a2k (9.0.6926) SP-3 Jet 4.0 SP-7)

    Hi John,

    Create the following procedure in the form module:

    Private Sub HandleKeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = (vbKeyDown And Shift = acAltMask) Or _
    KeyCode = vbKeyF4 Or KeyCode = vbKeyS Then
    Call lngID_DblClick(False)
    ElseIf KeyCode = vbKeyEscape Then
    DoCmd.Close acForm, Me.Name, acSaveNo
    End If
    End Sub

    You can call this in the On Key Down event procedure of multiple controls:

    Private Sub strCollectorCode_KeyDown(KeyCode As Integer, Shift As Integer)
    HandleKeyDown KeyCode, Shift
    End Sub

    Private Sub strDeposit_KeyDown(KeyCode As Integer, Shift As Integer)
    HandleKeyDown KeyCode, Shift
    End Sub

    Private Sub dtmRevDate_KeyDown(KeyCode As Integer, Shift As Integer)
    HandleKeyDown KeyCode, Shift
    End Sub

    Alternatively, if you want to execute this for ALL controls, you could set the KeyPreview property of the form to Yes, and write a single On Key Down event procedure for the Form.

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How To Put Code In A Sub? (a2k (9.0.6926) SP-3 Jet 4.0 SP-7)

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •