I have a requirement to filter continuous form using string entered or selected from combobox. Below is code I am attempting to use to capture the filter string. What happens is that when text is typed into the list, rather than the string being caught in the back, an error is instead thrown indicating that the combo-box is Null.
Where do I put this functionality? I am thinking of just adding the code to the Combobox_Selected event but that would not give user’s the ability to type arbitrary keywords with which to further filter the content of the form with.
Private Sub txtUSPSKeySearch_Change()
On Error GoTo Err_txtUSPSKeySearch_Change
Dim searchStr As String
searchStr = txtUSPSKeySearch.Value
If (Not IsNull(searchStr) And Len(searchStr) > 1) Then
Else
' Move the cursor to the end of the combo box.
Me.txtUSPSKeySearch.SetFocus
Me.txtUSPSKeySearch.SelStart = Len(Me.txtUSPSKeySearch.Value)
End If
'Error Handling
Exit_txtUSPSKeySearch_Change:
Exit Sub
Err_txtUSPSKeySearch_Change:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_txtUSPSKeySearch_Change
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_txtUSPSKeySearch_Change
End If
End Sub
“What happens is that when text is typed into the list, rather than the string being caught in the back, an error is instead thrown indicating that the combo-box is Null.“
You declared that
searchStrmust be String data type:When
txtUSPSKeySearchis Null, this assignment will fail because Null is not string data type.You could use the
Nz()function to givesearchStran empty string whentxtUSPSKeySearchis Null:Then you could change the
Ifstatement from this …to this …
You’re doing all this from the
txtUSPSKeySearchchange event. Consider using the before update event to validate the value, and the after update event to do whatever else (apply the filter condition?) you want to do with a valid value.