Updating a text field while typing

In Access, I have a form in which there are three text fields. I am trying to update a text box called tbxCombinedName using a combination of both:

  • textbox tbxLastName (person Surname)
  • textbox tbxFirstName (person's name)

My question is: what property of the text field I use, so when I type in tbxLastName, the CombinedName text field is updated immediately and then stored in the Contacts table.

At the Microsoft website , I found that the step process when entering a text field is as follows:

KeyDown → KeyPress → BeforeInsert → Edit → KeyUp

I tried using the OnChange and OnKeyDown properties, but to no avail. Which property, in combination with which code, will allow you to work with the update as you appear?

This is what I wrote earlier that didn't work:

Private Sub tbxLName_change()

Dim lastName As String
Dim nameCode As String

lastName = tbxLName.Value
Debug.Print lastName
nameCode = tbxNameCode.Value
nameCode = lastName
Debug.Print nameCode

End Sub

Thanks for your help in advance.

+5
source share
2 answers

This is one of the few cases where you must reference the .text property.

In the change event:

lastName = tbxLName.Text

The .text property is available only when the control has focus, and it refers to the visible content of the control.

However, this is a database, and the general rule is that you do not store calculated fields. Full name can be easily obtained from the request.

+6
source

Just a few notes:

, KeyPress, .

, - :

Private Sub tbxLName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Const ASCII_LOWER_RANGE = 65
        Const ASCII_UPPER_RANGE = 122
        Const ASCII_LOWER_A = 97
        Const ASCII_LOWER_Z = 122
        Const UPPER_MODIFIER = -32
        Const ASCII_CANCEL_CODE = 0

        Select Case KeyAscii
            Case ASCII_LOWER_RANGE To ASCII_UPPER_RANGE
                If KeyAscii >= ASCII_LOWER_A And KeyAscii <= ASCII_LOWER_Z Then
                    KeyAscii = KeyAscii + UPPER_MODIFIER
                End If
            Case Else
                KeyAscii = ASCII_CANCEL_CODE  'Cancel Key Press
        End Select
End Sub
+1

All Articles