Error Message: You can't reference a property or method for control unless the control has the focus...
Situation: I have a form that has a search textbox in the form header section that allows the users to dynamically search for items in a listbox. The code is in the OnChange event, this as the user types a letter, the listbox reduces in size as it finds matches. The user can then double click an item on the listbox and it will add a record to the form. The search function works perfectly fine as long as there's a record already in the form. However, when the form is open and there's no record, not talking about the listbox, but the detail section of the form. This may be related, but not certain. In the on open event, I have set that textbox set to receive focus. That textbox has focus upon opening the form when there's minimum one record, however, when there's no record, there nothing has focus that I can tell.
I have used two methods below. Both work fine as long as the form has a record in it.
1) This method, the listbox has a criteria (Like "*" & [forms]![myForm].[txtItemSearch] & "*") that looks at this textbox as it's been typed.
Code:
Dim vSearchString As String Dim bytCharacters As Byte
Me.lstBudget.Requery
Me.lstBudget = Me.lstBudget.ItemData(0)
Me.lstBudget.SetFocus
Me.txtItemSearch.SetFocus
bytCharacters = Len(Nz(Me.txtItemSearch))
If bytCharacters > 0 Then
Me.txtItemSearch.SelStart = bytCharacters
End If
2) This method, I change the rowsource and use a hidden textbox called txtSrchItemText
Code:
Dim vSearchItemString As String
Me.txtItemSearch.SetFocus
vSearchItemString = Nz(txtItemSearch.Text)
Dim strClientSQL As String
txtSrchItemText.Value = vSearchItemString
strClientSQL = "SELECT BudgetCode, BudgetItem FROM tblListBudgetCode WHERE BudgetItem= " & "'" & txtSrchItemText & "'"
Debug.Print strClientSQL
Me.lstBudget.RowSource = strClientSQL
Me.lstBudget = Me.lstBudget.ItemData(0)
Me.lstBudget.Requery