• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

TextBox, How does it work?

if i enter data into a textbox and DO NOT tab/cr the data cannot be passed, why? what is the work around ? e.g. type data into the textbox and then hit a button that execute programmed action without the need to activate the box with a tab or cr. thanx
1 Solution
Hitting enter on your text box can perform the action

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = 13 Then
    'do your code here
    MsgBox "Do SomeThing Here"
  End If
End Sub

Sorry but can you please provide more clarifications on the requirement so that I can help you out.

>... data cannot be passed, why?
I don't konw where you are trying to pass data ... but why dont you pass your data in the 'Change' event?

Please can you explain a little more?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


do you mean enter text in text box and press enter it should run code in a command click event? if so just set the default property of the command button to true.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        KeyAscii = 0 ' Suppresses the Beep
    End If
End Sub

Private Sub Command1_Click()
    Debug.Print Text1.Text
End Sub
Ok, its simple. Take the code below and cut and paste into a form named Form1 with a text box named Text1. That is all you need.
Then run the program, type whatever you want into the text box, then press Ctrl + c to get a message box of the contents of Text1.

Cut and Paste the code below into a form

Private Sub Form_Load()
    ' In order to select your keys you want to trap for, take a look at the Object Browser
    ' by pressing F2 in VB and then looking up these two constant sets
    ' "ShiftConstants"  (Used for Shift, Alt, and Ctrl)
    ' "KeyCodeConstants"   (Used for the Ascii characters A-Z, a-z, 0-9, etc. It will also show shift, alt, and ctrl. However
    ' if you press Ctrl + c the KeyCode will hold the value of 'c' and the ShiftConstant will hold the ShiftConstant for Ctrl.
    ' There is 1 tricky bit here. In the KeyCodeConstants Ctrl is vbKeyControl, Shift is vbKeyShift, BUT Alt IS NOT vbKeyAlt!
    '  It is vbKeyMenu.

    ' Do not use the KeyPress event, as it will only pass Ascii characters, A-Z and a-z and 0-9 etc...
    ' You wont get the value for shift, or ctrl, or alt etc...
    ' Use the Text1_KeyDown event, this way you can trap for the Alt, Ctrl, and Shift keys
    ' They keycode will be the constant value in the "KeyCodeConstants", and the shift will be the ShiftConstant
    ' If you wanted to trap someone pressing Ctrl + c and send the text somewhere look at the subroutine below
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

    ' Uncomment the lines below if you want to see what keys you are pressing
    'Debug.Print "KeyDown"
    'Debug.Print KeyCode
    'Debug.Print Shift

    If KeyCode = vbKeyC And Shift = vbCtrlMask Then
        MsgBox Form1.Text1.Text
    End If

End Sub

Good Luck,
I hope this is what you were looking for.
Jason Holland
I am interested. If there is more that I need to do please let me know.
Jason Holland
PAQed - no points refunded (of 200)

E-E Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now