Capture Key Codes within a Text Box

Posted on 2006-12-01
Medium Priority
Last Modified: 2008-03-03
I'm trying to capture certain key strokes within an Access application.  I've been using this function:

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

    Select Case KeyCode
      Case vbKeyDelete
        'User pressed the Delete key
    End Select

  End Sub

However, this only seems to work when they are not inside of a text box.  I'm looking to capture these key strokes when they are  entering data into a text box.  Is this possible?  Thanks in advance for your help.

Question by:cfg1980
  • 2
LVL 34

Expert Comment

ID: 18056347
Here's something close

Dim WholeString As String
Dim LengthString As Long
Private Sub data1_Change()
Dim myStr As String
Dim strPos As Long
strPos = Len(Me.data1.Value)
myStr = Right(Me.data1.Value, strPos - LengthString)
WholeString = WholeString & myStr
LengthString = strPos
End Sub
Private Sub data1_GotFocus()
WholeString = ""
LengthString = 0
End Sub
Private Sub data1_LostFocus()
MsgBox WholeString
End Sub
LVL 34

Accepted Solution

jefftwilley earned 500 total points
ID: 18056399
This one works too

Private Sub data1_KeyDown(KeyCode As Integer, Shift As Integer) ' Where Data1 is the name of your textbox
 Select Case KeyCode
      Case vbKeyDelete
        MsgBox "User pressed the Delete key"
    End Select

End Sub

Author Comment

ID: 18056485
That was exactly what I was looking for....thank you!

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

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

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
A quick solution showing how to control and open a POS Cash Register Drawer using VBA with MS Access.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question