SurreyNick
Member
- Local time
- Today, 02:15
- Joined
- Feb 12, 2020
- Messages
- 127
I am getting a 2185 error code when dynamically filtering records in a form from an unbound text box.
“Error… 2185 – You can’t reference a property or method for a control unless the control has focus”
The filter routine works fine until no records match the string in the text box and that’s when I get the error message which also locks up the form so I can’t continue and am forced to reload it.
I am only just getting to grips with VBA so I’m still at the stage of using code others have shared and don’t understand it well enough to debug/refine it effectively. I think what is happening is, that the lines of code which are meant to retain the search string entered into the text box and place the cursor at its end, is also populating the text box with the error message string and then holding onto that string locking the form up. The thing is these lines of code are needed in order to hold onto the search string after each application of the filter and cycle of the sub routine.
With my limited knowledge the only work around I have been able to come up with is to execute a reload of the form using the DoCmd method in the error handler section of the code and that’s not an ideal solution. Ideally I’d like to modify the code to avoid the Error 2185 message when zero records match the string and instead initiate a vbOKonly message box that alerts the user that there are no matching records and on clicking OK clears the text box ready to start again – obviously without locking the form up.
Can anyone help me with this?
The code routine is:
Private Sub txtNameFilter_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
If Len(txtNameFilter.Text) > 0 Then
filterText = txtNameFilter.Text
Me.Form.Filter = "[RecoverLostStudentQ]![Forename] Like '*" & filterText & "*' OR [RecoverLostStudentQ]![Surname] LIKE '*" & filterText & "*'"
Me.FilterOn = TRUE
txtNameFilter.Text = filterText
txtNameFilter.SelStart = Len(txtNameFilter.Text)
Else
Me.Filter = ""
Me.FilterOn = FALSE
txtNameFilter.SetFocus
End If
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
DoCmd.Close
DoCmd.OpenForm "RecoverLostStudent"
End Sub
“Error… 2185 – You can’t reference a property or method for a control unless the control has focus”
The filter routine works fine until no records match the string in the text box and that’s when I get the error message which also locks up the form so I can’t continue and am forced to reload it.
I am only just getting to grips with VBA so I’m still at the stage of using code others have shared and don’t understand it well enough to debug/refine it effectively. I think what is happening is, that the lines of code which are meant to retain the search string entered into the text box and place the cursor at its end, is also populating the text box with the error message string and then holding onto that string locking the form up. The thing is these lines of code are needed in order to hold onto the search string after each application of the filter and cycle of the sub routine.
With my limited knowledge the only work around I have been able to come up with is to execute a reload of the form using the DoCmd method in the error handler section of the code and that’s not an ideal solution. Ideally I’d like to modify the code to avoid the Error 2185 message when zero records match the string and instead initiate a vbOKonly message box that alerts the user that there are no matching records and on clicking OK clears the text box ready to start again – obviously without locking the form up.
Can anyone help me with this?
The code routine is:
Private Sub txtNameFilter_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
If Len(txtNameFilter.Text) > 0 Then
filterText = txtNameFilter.Text
Me.Form.Filter = "[RecoverLostStudentQ]![Forename] Like '*" & filterText & "*' OR [RecoverLostStudentQ]![Surname] LIKE '*" & filterText & "*'"
Me.FilterOn = TRUE
txtNameFilter.Text = filterText
txtNameFilter.SelStart = Len(txtNameFilter.Text)
Else
Me.Filter = ""
Me.FilterOn = FALSE
txtNameFilter.SetFocus
End If
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
DoCmd.Close
DoCmd.OpenForm "RecoverLostStudent"
End Sub