how to lock keyboard in excel vba

eepurchasekoradi 0 Reputation points
2025-12-01T05:21:14.1033333+00:00

vba coading to lock keyboard entry in excel vba text box input and allow only bar code scanner input

Microsoft 365 and Office | Excel | For education | Windows
{count} votes

2 answers

Sort by: Most helpful
  1. Jade Ng 6,060 Reputation points Microsoft External Staff Moderator
    2025-12-01T06:06:35.11+00:00

    Dear @eepurchasekoradi,

    Good day. Thank you for posting your question in the Microsoft Q&A forum!

    As per your description, I understand you're looking to lock user input in an Excel VBA text box so that it only accepts input from a barcode scanner.

    In this case, to ensure that you get professional help, I suggest you post on the Office VBA support and feedback for expert assistance. This is a specific channel related to VBA support. Please refer to the guidance in the article to obtain the best possible support.

    In this forum, you'll get the most qualified group of respondents, and other partners who read the forums regularly can share their knowledge or learn from your interaction. 

    Please notice and accept my apologies that as moderators in the community, we are not supported for the advanced info for Microsoft products. To help you send your feedback and requirement to the related development team, we have redirected you to the correct path. 

    I appreciate your patience and understanding and thank you for your time and cooperation. Please feel free to reply below if you have any update or further concern.  


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Q&A Assist
    2025-12-01T05:21:22.1066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To lock keyboard input in an Excel VBA TextBox and allow only barcode scanner input, you can use the KeyPress event of the TextBox control. This event allows you to filter the input based on the ASCII values of the characters. Typically, barcode scanners send input as a series of characters followed by an Enter key press.

    Here’s an example of how you can implement this:

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        ' Allow only numeric characters (ASCII 48-57) and control characters (like Enter)
        If Not (KeyAscii >= 48 And KeyAscii <= 57) And KeyAscii <> vbKeyReturn Then
            KeyAscii = 0 ' Cancel the key press
        End If
    End Sub
    

    In this code:

    • The KeyPress event checks if the pressed key is a numeric character (ASCII values 48 to 57) or the Enter key.
    • If the key pressed is not allowed, it sets KeyAscii to 0, effectively canceling the input.

    Make sure to replace TextBox1 with the actual name of your TextBox control. This way, the TextBox will only accept input from a barcode scanner that typically sends numeric values.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.