AltaSens
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.ActiveCon trol.Name) , 1)
intRowNumber = Mid(CStr(Screen.ActiveCont rol.Name), 3, Len(CStr(Screen.ActiveCont rol.Name)) - 2)
Select Case KeyCode
Case vbKeyDown
If intRowNumber < intNumRows Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
Forms!TimeEntry(strFormFie ldName).Se tFocus
Else
Screen.PreviousControl.Set Focus
End If
Case vbKeyUp
If intRowNumber > 1 Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
Forms!TimeEntry(strFormFie ldName).Se tFocus
Else
Screen.PreviousControl.Set Focus
End If
Case vbKeyLeft
If intColNumber > 1 Then
strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie ldName).Se tFocus
Else
Screen.PreviousControl.Set Focus
End If
Case vbKeyRight
If intColNumber < 7 Then
strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie ldName).Se tFocus
Else
Screen.PreviousControl.Set Focus
End If
End Select
End If
End Sub
__________________________
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.ActiveCon
intRowNumber = Mid(CStr(Screen.ActiveCont
Select Case KeyCode
Case vbKeyDown
If intRowNumber < intNumRows Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
Forms!TimeEntry(strFormFie
Else
Screen.PreviousControl.Set
End If
Case vbKeyUp
If intRowNumber > 1 Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
Forms!TimeEntry(strFormFie
Else
Screen.PreviousControl.Set
End If
Case vbKeyLeft
If intColNumber > 1 Then
strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
Else
Screen.PreviousControl.Set
End If
Case vbKeyRight
If intColNumber < 7 Then
strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
Else
Screen.PreviousControl.Set
End If
End Select
End If
End Sub
__________________________
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.
If so, you might try a Compact and Repair ... make a backup first, of course.
ASKER
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...?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.Acti veControl. Name), 1))
intRowNumber = CInt(Mid(CStr(Screen.Activ eControl.N ame), 3, Len(CStr(Screen.ActiveCont rol.Name)) - 2))
Select Case KeyCode
Case vbKeyDown
If intRowNumber < intNumRows Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
Forms!TimeEntry(strFormFie ldName).Se tFocus
End If
Case vbKeyUp
If intRowNumber > 1 Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
Forms!TimeEntry(strFormFie ldName).Se tFocus
End If
Case vbKeyLeft
If intColNumber > 1 Then
strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie ldName).Se tFocus
End If
Case vbKeyRight
If intColNumber < 7 Then
strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie ldName).Se tFocus
End If
End Select
End If
End Sub
--------------------------
--------------------------
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.Acti
intRowNumber = CInt(Mid(CStr(Screen.Activ
Select Case KeyCode
Case vbKeyDown
If intRowNumber < intNumRows Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
Forms!TimeEntry(strFormFie
End If
Case vbKeyUp
If intRowNumber > 1 Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
Forms!TimeEntry(strFormFie
End If
Case vbKeyLeft
If intColNumber > 1 Then
strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
End If
Case vbKeyRight
If intColNumber < 7 Then
strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
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°)
Success with your project!
(°v°)
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.ActiveCon
intRowNumber = Mid(CStr(Screen.ActiveCont
'Debug.Print intColNumber & "-" & intRowNumber
Select Case KeyCode
Case vbKeyDown
If intRowNumber < intNumRows Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber + 1)
Forms!TimeEntry(strFormFie
End If
Case vbKeyUp
If intRowNumber > 1 Then
strFormFieldName = CStr(intColNumber) & "-" & CStr(intRowNumber - 1)
Forms!TimeEntry(strFormFie
End If
Case vbKeyLeft
If intColNumber > 1 Then
strFormFieldName = CStr(intColNumber - 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
End If
Case vbKeyRight
If intColNumber < 7 Then
strFormFieldName = CStr(intColNumber + 1) & "-" & CStr(intRowNumber)
Forms!TimeEntry(strFormFie
End If
End Select
End If
End Sub
___________________