• AutoKeys Macro (A2000)

    Author
    Topic
    #372726

    I,m trying to disable the Left Arrow & Right Arrow keys by using the cancel event property in an AutoKey Macro.
    When I save the macro, I get a message saying the syntax or key combination is not allowed.

    I’m using {VbKeyLeft} as the syntax. Does anyone know either the correct way to do this.

    Dave

    Viewing 1 reply thread
    Author
    Replies
    • #596612

      You can’t use constants in macros. You have to use their numeric equivalent, which can be determined by looking them up in the object browser.

    • #596615

      Dave,

      You can’t reassign arrow keys in a macro. According to Access (97) help, the keys you can reassign are:

      Ctrl+letter
      Ctrl+digit
      Function keys Fn
      Ctrl+Fn
      Shift+Fn
      Ins
      Ctrl+Ins
      Shift+Ins
      Del
      Ctrl+Del
      Shift+Del

      Besides, it wouldn’t be a very good idea to cancel an arrow key in AutoKeys: it would mean that the keys would be disabled whatever you were doing in the database.

      ‘What you *can* do is disable them in a form. Set the KeyPreview property of the form to Yes/True, and create an event handler for the KeyDown event. Replace unwanted key codes by 0.

      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      Select Case KeyCode
      Case vbKeyLeft, vbKeyRight
      KeyCode = 0
      ‘ Case …
      ‘ …
      End Select
      End Sub

      • #596617

        I notice Hans you use “select Case”, maybe I need to do the same with my code:

        Private Sub cmbClientCode_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyDown Then Me.CmbRegistration.SetFocus
        If KeyCode = vbKeyUp Then Me.cmbInsurerCode.SetFocus
        If KeyCode = vbKeyF11 Then
        If Me.cmbClientCode.ListIndex > 0 Then
        Me!cmbClientCode.ListIndex = Me!cmbClientCode.ListIndex – 1
        End If
        End If
        If KeyCode = vbKeyF12 Then
        If Me.cmbClientCode.ListIndex < Me.cmbClientCode.ListCount – 1 Then
        Me!cmbClientCode.ListIndex = Me!cmbClientCode.ListIndex + 1
        End If
        End If
        If KeyCode = vbKeyF2 Then Call cmbClientCode_DblClick(1)
        End Sub

        I use quite a few KeyCode combinations, can you steer me in the right direction here. I needed to use the VbKeyRight & Left to replace the F11 & F12 parts of the code, but when I do, I get odd happenings.
        I assume the Access default action for the keys is kicking in at the same time as I call them through code.

        Dave

        • #596620

          Dave,

          Using “Select Case” will certainly make your code more readable.

          There are several keystrokes that Access will handle automatically (arrow keys, tab key, function keys), unless you cancel them by setting the KeyCode to 0 after you have done your own processing.

          For example:

          Private Sub cmbClientCode_KeyDown(KeyCode As Integer, Shift As Integer)
          Select Case KeyCode
          Case vbKeyDown
          Me.CmbRegistration.SetFocus
          KeyCode = 0
          Case vbKeyUp
          Me.cmbInsurerCode.SetFocus
          KeyCode = 0
          Case vbKeyF11
          If Me.cmbClientCode.ListIndex > 0 Then
          Me!cmbClientCode.ListIndex = Me!cmbClientCode.ListIndex – 1
          KeyCode = 0
          End If
          Case vbKeyF12
          If Me.cmbClientCode.ListIndex < Me.cmbClientCode.ListCount – 1 Then
          Me!cmbClientCode.ListIndex = Me!cmbClientCode.ListIndex + 1
          KeyCode = 0
          End If
          Case vbKeyF2
          Call cmbClientCode_DblClick(1)
          KeyCode = 0
          End Select
          End Sub

          • #596633

            Thanks again Hans.

            This makes things much clearer, ( Have a lot of editing to do noe.)

            Dave

            • #596644

              Sorry to be a pain, one final thing.
              If I want to use lets say VbKeyF3 to do more than one task on the same form.
              Which is the correct way, Example 1 or Example 2
              (Example 1)
              Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
              On Error GoTo ERRTRAP
              Select Case KeyCode
              Case vbKeyF5
              Call Command19_Click
              KeyCode = 0
              Case vbKeyF3
              Me.sbfLabourBooking.SetFocus
              KeyCode = 0
              Case vbKeyF3
              DoCmd.GoToRecord , , acNewRec
              KeyCode = 0
              Case vbKeyF3
              Me.EstimateNo = Forms!frmlabourbooking!txtSearchEstimateNo.Value
              KeyCode = 0
              Case vbKeyF3
              Me.supp = Forms!frmlabourbooking!txtSearchSupp.Value
              KeyCode = 0
              End Select
              Exit Sub
              ERRTRAP:
              End Sub

              (Example 2)
              Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
              On Error GoTo ERRTRAP
              Select Case KeyCode
              Case vbKeyF5
              Call Command19_Click
              KeyCode = 0
              Case vbKeyF3
              Me.sbfLabourBooking.SetFocus
              DoCmd.GoToRecord , , acNewRec
              Me.EstimateNo = Forms!frmlabourbooking!txtSearchEstimateNo.Value
              Me.supp = Forms!frmlabourbooking!txtSearchSupp.Value
              KeyCode = 0
              End Select
              Exit Sub
              ERRTRAP:
              End Sub

              thanks again
              Dave

            • #596756

              Only the first case will ever be executed if you put it in twice, so the second example is the only way it will every work. However, I would suggest that you cancel the keycode by setting it to 0 *before* you do any of the other stuff in the case. Otherwise, it might bite you.

            • #596803

              Thanks all, I can take it from here.

              Dave

    Viewing 1 reply thread
    Reply To: AutoKeys Macro (A2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: