Disable arrow keys (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
Hello, I have a database that has on click events in some textboxes and they have to be used. The problem I have is I have an employee that uses the Keyboard TAB and the keyboards arrow keys to get around this and even being told not to they continue to do so. So I needed to disable these on my form (Tab and arrow keys) I am able to disable the tab but how do I stop the arrow keys? Can someone please show me an example from my code below? Thanks!!!

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

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyTab
            KeyCode = 0
            MsgBox "The Tab feature has been removed to prevent manual entries instead of using popup box entries!!", vbCritical, "Tab Disabled"
    End Select
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:40
Joined
Oct 29, 2018
Messages
21,358
Try using:
Code:
...
    Case vbKeyTab, 37 To 40
        KeyCode = 0
...

 

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
That did it thanks.... Oh I just found out enter moves it also, how do I disable that key as well
 

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
Thanks all that did it..... Below is code if someone needed it....

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

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab
        KeyCode = 0
        MsgBox "Sorry the Tab feature has been disabled to prevent manual entries instead of using popup box entries!!", vbCritical, "Tab Key Disabled"
        
    Case vbKeyF11
        KeyCode = 0
        MsgBox "Sorry, F11 has been disabled to prevent manual entries instead of using popup box entries!!", vbOKOnly, "F11 Key Disabled"
        
    Case 37 To 40
        KeyCode = 0
        MsgBox "Sorry, arrow keys has been disabled to prevent manual entries instead of using popup box entries!!", vbOKOnly, "Arrow Keys Disabled"
        
    Case 13 'Enter-Key
        KeyCode = 0
        MsgBox "Sorry, the enter keys has been disabled to prevent manual entries instead of using popup box entries!!", vbOKOnly, "Enter Keys Disabled"
        
    End Select
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:40
Joined
Oct 29, 2018
Messages
21,358
Glad to hear you got it sorted out. Good luck with your project.
 

cheekybuddha

AWF VIP
Local time
Today, 14:40
Joined
Jul 21, 2014
Messages
2,238
Wouldn't you be better off locking the controls, and testing for any key entry other than navigation; then use that to open up your pop up entry form?

eg Something like:
Code:
Private Sub YourTextbox_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode
    Case vbKeyTab, vbKeyF11, 37 To 40, 13 'Enter-Key
        ' Do nothing, allow navigation as normal
    Case Else
        KeyCode = 0
        DoCmd.OpenForm "YourEditForm", , , , , acDialog      
    End Select
   
End Sub

I know I'd hate it if an app prevented me from normal expected usage.
 

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
I hate to block all the controls because there is only 4 controls that need need this (txtSLegCS, txtSLegNCS, txtPLegCS, txtPLegNCS).

Is there a way I can just block tabbing and arrow keys to these controls and within these controls because they are one after another.
 

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
What code do you have in the click events of those controls?

David, I used your example BUT I'm able to tab to the first control from a control that has no restrictions if I were to go this route of restricting the 4 text boxes that are in a row. Lets say I have a text box txt1 and then next text box is txtSLegCS, Im able to tab from txt1 to txtSLegCS but cant tab or use arrow keys after that and same thing for the last box txtPLegCS. If I had a textbox txt6 and tried to arrow up to txtPLegCS it would let me but not from there. I need to block going to and within these 4 boxes....

Code:
Private Sub txtSLegCS_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyF11, 37 To 40, 13 'Enter-Key
        KeyCode = 0
    Case Else
        'Do nothing
    End Select
End Sub

Private Sub txtSLegNCS_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyF11, 37 To 40, 13 'Enter-Key
        KeyCode = 0
    Case Else
        'Do nothing
    End Select
End Sub

Private Sub txtPLegCS_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyF11, 37 To 40, 13 'Enter-Key
        KeyCode = 0
    Case Else
        'Do nothing
    End Select
End Sub

Private Sub txtPLegNCS_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyF11, 37 To 40, 13 'Enter-Key
        KeyCode = 0
    Case Else
        'Do nothing
    End Select
End Sub

Oh and love the dog.....
 

cheekybuddha

AWF VIP
Local time
Today, 14:40
Joined
Jul 21, 2014
Messages
2,238
My suggestion was to allow tabbing in to (and out of) those controls as normal.

However, if their .Locked property is set to True, then the user can't edit using the keyboard.

Capture the key event for any key pressed other than a tab, arrow or Enter, and trigger the same code that you have in the click event to open the pop up editor for the control.
 

cheekybuddha

AWF VIP
Local time
Today, 14:40
Joined
Jul 21, 2014
Messages
2,238
I may be misunderstanding the reason why you don't want the user to tab into those controls. I thought it was because they would then edit the values directly and without using a pop up editor.

If I'm mistaken, perhaps an alternative would be set the control's .TabStop property to False (or 'No' in the 'Other' tab of the property sheet in design view)

Then, the control is skipped when tabbing or using the arrow keys; no code required.
 

oxicottin

Learning by pecking away....
Local time
Today, 10:40
Joined
Jun 26, 2007
Messages
851
Your post #12 resolved my issue. There is a On_Click event in each of the 4 text boxes. I attached an example of the code
 

Attachments

  • Database1.zip
    27.4 KB · Views: 264

cheekybuddha

AWF VIP
Local time
Today, 14:40
Joined
Jul 21, 2014
Messages
2,238
Your post #12 resolved my issue.
Did you manage to resolve this yourself? I read the above and thought you had, but I looked at the attachment this morning and it seems to reflect your code in post #11.

I made 2 quick variations of your form to demonstrate the ideas in post #12 and post #13. See if that helps.
 

Attachments

  • Database1_revised.zip
    71.8 KB · Views: 215

Users who are viewing this thread

Top Bottom