Obtaining textbox value in change event handler

前端 未结 6 735
攒了一身酷
攒了一身酷 2021-01-13 04:10

I\'ve written a form that performs queries asynchronously as text is typed into a textbox, however I somewhat arbitrarily seem to get the following error th

6条回答
  •  隐瞒了意图╮
    2021-01-13 04:31

    I added a text box named txtFoo to my form. Here is the procedure for its change event.

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

    Then, with nothing in txtFoo (IOW its value is Null) when I type "abc" into txtFoo, here is what I see in the Immediate window.

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

    Basically, each character I add to the text box triggers its change event and prints the text box's current contents to the Immediate window.

    As far as I understand, you want to do something similar ... except you want a different action in place of Debug.Print. Take another look at your change event procedure and compare it to mine.

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

    That is the change event for a control named txtBox. Within that procedure you reference the .Text property of a control named txtVendorName. However txtBox is the active control at the time its change event code runs ... so you can not access the .Text property of txtVendorName because it is not the active control.

    Given that this problem surfaces for only the one form, but not on other new forms, I would suspect the problem form has become corrupted. Read the 2 answers to this SO question and try decompile to cure the corruption: HOW TO decompile and recompile. Decompile is often recommended as a routine practice during development.

    You could also use the undocumented Application.SaveAsText method to save your form as a text file. Delete the bad form, and use Application.LoadFromText to import the saved text copy.

    Make sure you have a backup copy of your db file in case anything goes wrong.

提交回复
热议问题