Paolo Bernasconi
Paolo Bernasconi

Reputation: 2030

Cannot reference to a form control which is out of focus

This is a follow up to a previous question I asked about how to update a textbox while typing. In my previous question, I had three textboxes, two of which were enabled and one which was not. They look like this:

Three textbox image

My goal is to use the First and Last Name of the two textboxes to fill a "Code Personal" in the other textbox. As I type the first and last names, I want the Code Personal textbox to update immediately, with the format of LLLLLL_F (eg: BERNAS_P). However, whenever I try to update the Code Personal textbox, I receive this error:

Error image

The code I use to create the Code Personal format and to update the textbox is:

Private Sub TxtFName_Change()

firstName = Me.TxtFName.Value
lastName = Me.txtLName.Value
firstPart = Left(lastName, 6)
secondPart = Left(firstName, 1)

nameCode = firstPart + "_" + secondPart
upperNameCode = UCase(nameCode)

txtCodePersonal.Text = upperNameCode 'My debug tells me I have an error here
End Sub

I've tried to set the focus to the txtCodePersonal textbox through: [txtCodePersonal].SetFocus, but I still receive an error (MS Access can't move the focus to the control txtCodePersonal.)

Any Ideas as to how I can update the "Code Personal" textbox while typing in the other two textboxes?

Thanks in advance for all your help.

Upvotes: 1

Views: 2574

Answers (1)

HansUp
HansUp

Reputation: 97131

Do your value assignment to the text box's .Value property instead of its .Text property. Either of these should work:

Me.txtCodePersonal.Value = upperNameCode
Me.txtCodePersonal = upperNameCode

.Value is the default property for a text box, so you don't need to include it explicitly. Include it if you feel it makes the code clearer.

Upvotes: 2

Related Questions