Getting the value of a text field in a change event handler

I wrote a form that executes requests asynchronously when text is typed into a text field, but I usually seem to cause the following error: "You cannot reference a property or method for control if the control has no focus."

Directly relevant code:

Dim UpdateRequested As Boolean
Dim qryText As String

.
.
.

Private Sub txtBox_Change()
    qryText = txtBox.Text
    UpdateRequested = true
End Sub

Some place in ellipses is code that handles dynamically loading a set of ADODB records, populating a local table, and updating a subform. However, even when I turned off this code, the problem persists: sometimes I get an error. Sometimes I don’t do it.

. , , , , , " " .

Update

, :

Private Sub txtBox_GotFocus()
    MsgBox "Got focus"
End Sub

Private Sub txtBox_LostFocus()
    MsgBox "Lost focus"
End Sub

. . "Got focus". , , . , ( ) " " " ", . , "Screen.ActiveControl.Name" Text.

+5
6

, , , , . , , . , . , Kaganar:

  • ( ).
  • .

, ...

Text ,

, Access, , :) , ...

  • TextBox
  • .

, ! .

, et Voila! .

+6

txtFoo . .

Private Sub txtFoo_Change()
    Debug.Print "Value: " & Nz(Me.txtFoo.value, "*Null*") & _
        "; Text: " & Nz(Me.txtFoo.Text, "*Null*")
End Sub

txtFoo ( IOW Null), "abc" txtFoo, Immediate.

Value: *Null*; Text: a
Value: *Null*; Text: ab
Value: *Null*; Text: abc

, , , Immediate.

, - ... , Debug.Print . .

Private Sub txtBox_Change()
    qryText = txtVendorName.Text
    UpdateRequested = true
End Sub

txtBox. .Text txtVendorName. txtBox ... .Text txtVendorName, .

, , , , . 2 SO , : . .

Application.SaveAsText . Application.LoadFromText .

, db , - .

+5

Text , .
, txtBox.SetFocus DoCmd.GoToControl "txtBox".

, Text :
, Text , ; Value . , , Value . Text , .

+3

. , , , , .

+1

" " . , , , .

Access , : a) " " " " ) , ( ) c) .

"" . , Access .

: http://allenbrowne.com/bug-06.html

0

I know my answer is out of date. However, you can adjust the focus three times. In the TextBox in the header, on any text box in the text box and in the text box in the header again. I am using access 2003.

-1
source

All Articles