Link to home
Start Free TrialLog in
Avatar of AltaSens
AltaSensFlag for United States of America

asked on

over-ride default arrow key form navigation behaviour

I am using the code listed below to allow users to navigate between controls in a form which is layed out in a grid (sort of like Excel) using the arrow keys.  This works except for when the current focus is at the edge of the grid (i.e. top/bottom row, leftmost/rightmost column) of controls then the default arrow key behaviour takes over the up/left keys goto the previous control in the taborder, right/down goto the next control.

Is there any way to over-ride this default key navigation behaviour?

THANKS!


__________________________
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Screen.ActiveControl.Name Like "#-#" Or Screen.ActiveControl.Name Like "#-##" Then

        intColNumber = Left(CStr(Screen.ActiveControl.Name), 1)
        intRowNumber = Mid(CStr(Screen.ActiveControl.Name), 3, Len(CStr(Screen.ActiveControl.Name)) - 2)
       
        Select Case KeyCode

            Case vbKeyDown
                If intRowNumber < intNumRows Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                Else
                    Screen.PreviousControl.SetFocus
                End If
         
            Case vbKeyUp
                If intRowNumber > 1 Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                Else
                    Screen.PreviousControl.SetFocus
                End If
       
            Case vbKeyLeft
                If intColNumber > 1 Then
                    strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                Else
                    Screen.PreviousControl.SetFocus
                End If
       
            Case vbKeyRight
                If intColNumber < 7 Then
                    strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                Else
                    Screen.PreviousControl.SetFocus
                End If

        End Select

    End If
 
End Sub
__________________________

Avatar of AltaSens
AltaSens
Flag of United States of America image

ASKER


I had an idea and disabled the tab stops for all of the text box controls on my form.  I modified the code slightly to what is listed below.

NOW what's happening is that I can navigate through the "grid" using the left, right, and up keys but the DOWN key now doesn't do anything.  I verified that the event is firing and that Access is selecting the right case (by using a msgbox) but for some reason, I can't change the control focus.

!*()%)(*!@)(&*!!!

any ideas here?

___________________
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    If Screen.ActiveControl.Name Like "#-#" Or Screen.ActiveControl.Name Like "#-##" Then
       
        intColNumber = Left(CStr(Screen.ActiveControl.Name), 1)
        intRowNumber = Mid(CStr(Screen.ActiveControl.Name), 3, Len(CStr(Screen.ActiveControl.Name)) - 2)
       
        'Debug.Print intColNumber & "-" & intRowNumber
       
        Select Case KeyCode

            Case vbKeyDown
                If intRowNumber < intNumRows Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
         
            Case vbKeyUp
                If intRowNumber > 1 Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
            Case vbKeyLeft
                If intColNumber > 1 Then
                    strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
            Case vbKeyRight
                If intColNumber < 7 Then
                    strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
        End Select
 
    End If
 
End Sub
___________________
Avatar of Scott McDaniel (EE MVE )
So it actually gets to your Case vbKeyDown block, and it runs through that block? And the vbKeyUp works?

If so, you might try a Compact and Repair ... make a backup first, of course.
Yes, the Case vbKeyDown block does indeed execute.  all of the other keys work.  Key Preview is on as this is a form event procedure.

I tried a compact & repair, that didn't change anything.

I wonder if there is something happening at the control level which is over-riding the setfocus...?

Is there in code in the Enter, GotFocus or Activate events of the control?
ASKER CERTIFIED SOLUTION
Avatar of Markus Fischer
Markus Fischer
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there is no code in the Enter, GotFocus, or Activate events.

This is the only place I am using intNumRows, this is used to set the maximum number of rows available and the value comes from a rs query.  

THANKS harfang for pointing this out, I made a bone-head mistake and the value comparison in that block failed because I was comparing a integer to a string.  It's fixed and works now.
Here is the final code block that now works.

--------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    If Screen.ActiveControl.Name Like "#-#" Or Screen.ActiveControl.Name Like "#-##" Then
       
        intColNumber = CInt(Left(CStr(Screen.ActiveControl.Name), 1))
        intRowNumber = CInt(Mid(CStr(Screen.ActiveControl.Name), 3, Len(CStr(Screen.ActiveControl.Name)) - 2))
       
        Select Case KeyCode

            Case vbKeyDown
                If intRowNumber < intNumRows Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
         
            Case vbKeyUp
                If intRowNumber > 1 Then
                    strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
            Case vbKeyLeft
                If intColNumber > 1 Then
                    strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
            Case vbKeyRight
                If intColNumber < 7 Then
                    strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
                    Forms!TimeEntry(strFormFieldName).SetFocus
                End If
       
        End Select
 
    End If
 
End Sub
--------------------------
Thank you for the feedback, and I'm glad this was something simple, in the end.
Success with your project!
(°v°)