Any good ideas for Left & Right keypress not using arrows

bignose2

Registered User.
Local time
Today, 01:42
Joined
May 2, 2010
Messages
248
Hi,

In my forms I would often like to move forward or back a records, usually a date by keyboard alone.

The keyboard arrows are good but they are normally used to navigate along a line etc.

Page Up & Down again better & often used for moving between records but do sometime need there native functions.

Trying to avoid changing the functions of these keys depending of what part fo the form I am on or current function I an doing.

It would have been great if there was a Alt Left or Ctrl Left that gave a different ansii.

Alt N - next, Alt P - previous, not pretty.

Any ideas
Thanks I/A
 
I'm not sure if this will satisfy you, but in your forms On Key Down event you can put coding in for the up and down arrow keys to allow movement between records:

for Form, add
Dim lastRecord As Integer

Code:
If Me.CurrentRecord > 1 Then
    If KeyCode = vbKeyUp Then
        If lastRecord = 0 Then
        DoCmd.GoToRecord , , acPrevious
        Exit Sub
        Else
        DoCmd.GoToRecord , , acLast
        lastRecord = 0
        Exit Sub
        End If
    End If
End If

If KeyCode = vbKeyDown Then
    If Me.CurrentRecord < Me.RecordsetClone.RecordCount Then
        DoCmd.GoToRecord , , acNext
        lastRecord = 0
        Exit Sub
        Else
        lastRecord = 1
        DoCmd.GoToRecord , , acNewRec
        Exit Sub
    End If
End If

You will need to turn on the forms Key Preview to enable capturing the keystrokes. If you don't want to use the up/down arrow keys, you can substitute any keystroke,i.e, ^Z for up and ^X for down:

Code:
If Me.CurrentRecord > 1 Then
    If Shift = 2 And (KeyCode = 16 or KeyCode = 90) Then 'this is control z or control Z
        If lastRecord = 0 Then
        DoCmd.GoToRecord , , acPrevious
        Exit Sub
        Else
        DoCmd.GoToRecord , , acLast
        lastRecord = 0
        Exit Sub
        End If
    End If
End If

If Shift = 2 And (KeyCode = 16 Or KeyCode = 88) Then 'this is control x or control X
    If Me.CurrentRecord < Me.RecordsetClone.RecordCount Then
        DoCmd.GoToRecord , , acNext
        lastRecord = 0
        Exit Sub
        Else
        lastRecord = 1
        DoCmd.GoToRecord , , acNewRec
        Exit Sub
    End If
End If
 
Many thanks for your suggestions,

I often use combo boxes with drop down and use the up & down arrow for these & if on the same form causes problems.

I was trying to find a good system for all my programs & keep consistant with something fairly obvious for other users also.

I think the Z & X might be the simplest & safest way, shame there is no codes for Ctrl or Alt on the Arrows or + & - etc.

Thanks again.
 
I believe you could use ctl or alt with the arrows. In the KeyDown event you are capturing both the KeyCode and Shift code. Just put them together in whatever combination and it should work. The Shift Codes are:

The Keycode value represents the key that triggered the event.

Shift Argument for the 7 combinations of Shift, Ctrl and Alt keys:
0 None
1 Shift Key
2 Ctrl Key
3 Shift + Ctrl Keys
4 Alt Key
5 Shift + Alt Keys
6 Ctrl + Alt Keys
7 Shift + Ctrl + Alt Keys
 
Here is code for testing in combobox is active:

Code:
Option Compare Database

'******* Code Start *********
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'  retrieves the name of the class to which the specified window belongs.
Private Declare Function apiGetClassName Lib "user32" _
    Alias "GetClassNameA" _
    (ByVal hwnd As Long, _
    ByVal lpClassname As String, _
    ByVal nMaxCount As Long) _
    As Long

'  retrieves a handle to the specified child window's parent window.
Private Declare Function apiGetParent Lib "user32" _
    Alias "GetParent" _
    (ByVal hwnd As Long) _
    As Long

'  retrieves information about the specified window. The function also
'  retrieves the 32-bit (long) value at the specified offset into the
'  extra window memory of a window.
Private Declare Function apiGetWindowLong Lib "user32" _
    Alias "GetWindowLongA" _
    (ByVal hwnd As Long, _
    ByVal nIndex As Long) _
    As Long

'  retrieves a handle to the top-level window whose class name and
'  window name match the specified strings. This function does not search
'  child windows. This function does not perform a case-sensitive search.
Private Declare Function apiFindWindow Lib "user32" _
    Alias "FindWindowA" _
    (ByVal lpClassname As String, _
    ByVal lpWindowName As String) _
    As Long

'  retrieves a handle to a window that has the specified relationship
'  (Z order or owner) to the specified window
Private Declare Function apiGetWindow Lib "user32" _
    Alias "GetWindow" _
    (ByVal hwnd As Long, _
    ByVal wCmd As Long) _
    As Long

'  The class name for an Access combo's drop down listbox window
Private Const ACC_CBX_LISTBOX_CLASS = "OGrid"
'  Class name for the Access window
Private Const ACC_MAIN_CLASS = "OMain"
'  Class name for an Access combo's drop down listbox's parent window
Private Const ACC_CBX_LISTBOX_PARENT_CLASS = "ODCombo"
'  class name for an Access form's client window
Private Const ACC_FORM_CLIENT_CLASS = "OFormSub"
'  class name for Edit controls in Access
Private Const ACC_CBX_EDIT_CLASS = "OKttbx"
'  class name for VB combo's drop down listbox's parent window (SDI)
Private Const VB_CBX_LISTBOX_PARENT_CLASS = "#32769" ' // Desktop
'  class name for VB combo's drop down listbox window
Private Const VB_CBX_LISTBOX_CLASS = "ComboLBox"
'  handle identifies the child window at the top of the Z order,
'  if the specified window is a parent window
Private Const GW_CHILD = 5
'  Retrieves the window styles.
Private Const GWL_STYLE = (-16)
'  flag denoting that a window is visible
Private Const WS_VISIBLE = &H10000000

Function fIsComboOpen() As Boolean
'  returns true if a combo box on the form is dropped down
'  only one combo can have the focus => only one drop down
'
Static hwnd As Long
Static hWndCBX_LBX As Long

   hwnd = 0: hWndCBX_LBX = 0

   '  Start with finding the window with "ODCombo" class name
   hwnd = apiFindWindow(ACC_CBX_LISTBOX_PARENT_CLASS, _
                                vbNullString)
   '  Parent window of ODCombo is the Access window
   If apiGetParent(hwnd) = hWndAccessApp Then
         '  Child window of ODCombo window is the
         '  drop down listbox associated with a combobox
         hWndCBX_LBX = apiGetWindow(hwnd, GW_CHILD)
         '  another check to confirm that we're looking at the right window
         If fGetClassName(hWndCBX_LBX) = _
                        ACC_CBX_LISTBOX_CLASS Then
            '  Finally, if this window is visible,
            If apiGetWindowLong(hwnd, GWL_STYLE) And WS_VISIBLE Then
               '  the Combo must be open
               fIsComboOpen = True
            End If
         End If
      End If
End Function

Private Function fGetClassName(hwnd As Long)
Dim strBuffer As String
Dim lngLen As Long
Const MAX_LEN = 255
    strBuffer = Space$(MAX_LEN)
    lngLen = apiGetClassName(hwnd, strBuffer, MAX_LEN)
    If lngLen > 0 Then fGetClassName = Left$(strBuffer, lngLen)
End Function
'******* Code End *********

In your KeyDown event, place this code prior to any other code:

If fIsComboOpen() = True Then Exit Sub
 
To be honest, I'm confused as to exactly what you want, with your original question, but if your object is to use the Up and Down Arrows along with, say, the <Control> Key, as well:

Code:
Private Sub Form_Load()
  Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 If Shift = acCtrlMask And KeyCode = vbKeyUp Then
  MsgBox "<Ctrl> + <Up Arrow> Used!"
  KeyCode = 0
 End If
 
 If Shift = acCtrlMask And KeyCode = vbKeyDown Then
  MsgBox "<Ctrl> + <Down Arrow> Used!"
  KeyCode = 0
 End If
End Sub

Just replace the Messageboxes, in the code, with the actual code you want to use.

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom