Event Action upon keystroke with 2 keys

Dudley

Registered User.
Local time
Today, 05:41
Joined
Apr 7, 2004
Messages
147
I've been trying to understand and get this to work all day. I've been all over this forum and Google. I've found a lot of material and come up with some code and workarounds, but something is still not right.

I confess, I'm trying to set-up navigating with the tab key and shift+tab between a main form and two subforms. I've learned that control+tab will move out of the subform and onto the main form "forward" on the tab indexes and that control+shift+tab will move onto the main form "backward" according to the tab indexes.

I've also learned that for some reason Access often skips the first tab stop outside the subform and moves to the next one. So I've added almost invisible text boxes around the subforms to absorb this and let the tab rest on the desired control - the next one outside of the subform. It looks a bit funny to have the cursor stop on un-otherwise-noticeable-places-on-the-form. I haven't found an explanation for this skipping thing, though. ??

I'm trying to trap the shift+tab keystroke action in the combobox that is the only control on the first subform so I can send the user to the "previous" control on the main form. When I do it myself it works fine, but when I try to use some version of sendkeys the cursor just stays in the combo box.

I've been trying to come up with some keystroke arithmetic to set a value for a variable to determine what sendkeys action comes next. But I can't get the math to work.

I'm working within the KeyDown event of the only control on the subform, a combobox.

Code:
Private Sub cboMedicalCondition_KeyDown(KeyCode As Integer, Shift As Integer)
end sub

As I've tried to figure it out, I'm noticing that the "keycode" value from the KeyDown event is equal to 16 if I'm trying to shift+tab and is equal to 9 if I'm just hitting tab. Checking this out, looks like the shift key is "worth" 16 and the tab key is "worth" 9. So, KeyDown is recognizing only the first key hit, just the Shift. How can I get something that will catch shift+tab?

I also found in the Access Help File the following code:

Code:
    Dim intShiftDown As Integer, intAltDown As Integer
    Dim intCtrlDown As Integer

    ' Use bit masks to determine which key was pressed.
    intShiftDown = (Shift And acShiftMask) > 0
    intAltDown = (Shift And acAltMask) > 0
    intCtrlDown = (Shift And acCtrlMask) > 0
    ' Display message telling user which key was pressed.
    If intShiftDown Then MsgBox "You pressed the SHIFT key."
    If intAltDown Then MsgBox "You pressed the ALT key."
    If intCtrlDown Then MsgBox "You pressed the CTRL key."

From which I've learned that the acShiftMask is "worth" 1, acAltMask is "worth" 4, and acCtrlMask is "worth" 2.

I thought a Select Case approach might be a good idea. Here's what I have that doesn't work:

Code:
Dim lngValue As Integer
lngValue = KeyCode + Shift   'These are the variables in the KeyDown event
    
    Select Case lngValue
        Case 9   'tab key alone = 9
            KeyCode = 0
            SendKeys "^{tab}"
        Case 10   'tab key = 9; shift key = 1
            KeyCode = 0
            SendKeys "^+{tab}"
        Case Else
            KeyCode = 0
            SendKeys "^{tab}"
    End Select

Another approach that isn't working is:

Code:
If KeyCode = vbKeyTab Then
        KeyCode = 0
        SendKeys "^{tab}"
    End If
    If KeyCode + Shift = vbKeyTab + vbKeyShift Then  'Here's a place where the math doesn't work
        SendKeys "^+{tab}"
        Me.Parent.txtTabMgt1.SetFocus  'This SetFocus approach isn't working either, when I try it alone
    End If

Could someone please help me understand this better? I seem to be stuck in a vortex here. Thanks!!!
 
.
I was always told never to use send keys, or any of the key trapping type methods of doing things. You should be able to find some way of doing this in code. other than that I can't help you.

It's just I think you are barking up the wrong tree, Unfortunately I haven't needed to set up a keyboard interface, so I am unable to point you to the right tree!
 
Uncle Gizmo,
Thanks for your great post. Talk about feeling supported. It's useful to me to hear about your being told not to use SendKeys, and your sense I was barking up the wrong tree.

After consolidating what I knew to write the post, I felt I ought to be able to put it together. Here's what I came up with, using the "unseen" textbox controls to "absorb" the float to the next tab index in either direction.

I did some further checking and found that "Shift" ends up with the same values as the key constants (1, 2, and 4). So I was really almost there.

Code:
    Dim lngValue As Integer
    lngValue = KeyCode + Shift
    
    Select Case lngValue
        Case 9
            Me.Parent.txtTabMgt2.SetFocus
        Case 10
            Me.Parent.txtTabMgt1.SetFocus
        Case Else
            Me.Parent.txtTabMgt2.SetFocus
    End Select

I really appreciate your post. Thanks!
 
The only way to learn I reckon!

Hi Dudley,
Re: >>>After consolidating what I knew<<

That's the only way to learn I reckon! Well that's what I do anyway, I build on what I already know, I watched the posts for questions that interest me, and look at the answers and very often learn quite a bit that way!

I just thought I'd mention, by way of offering you a different solution that you may find useful. By the way it's not a better solution, in any real sense, it's just a more satisfying one!

You said: "unseen" textbox controls --- you may be able to replace these with custom properties. Like I said it's not necessary, however it's an interesting way of doing it.

The following code shows how to create a custom property called "prpMatrixID" I find custom properties very useful, you can set them whilst opening the form, in other words pass information into your form without using the openargs, and then a "complicated" routine for extracting the data! Much tidier with custom properties in my opinion.

You also expose whatever values are in the custom properties to other forms, and also reports, so you can run a report and extract information from the form controlling the report, like the SQL for the report for instance.

Code:
'DECLARATIONS SECTION ------------------------------------------------------------------------------------

Private mlngMatrixID As Long

Property Let prpMatrixID(lngID As Long)
'this property is set by the
    mlngMatrixID = lngID
End Property      'prpMatrixID Let

Property Get prpMatrixID() As Long
    prpMatrixID = mlngMatrixID
End Property      'prpMatrixID Get
'
'End of general block of property setting code
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 

Users who are viewing this thread

Back
Top Bottom