Link to home
Start Free TrialLog in
Avatar of GPSPOW
GPSPOWFlag for United States of America

asked on

Creating a text box to search a list box

I am tryng to create a text box to search through a list box that will scroll through the list box and display the records that return those records matching the "LastName" values "Like" or "contains" the letters input:  i.e.

If the user inputs "Ada"  the list box would display "Adams" and below.

Here is the code I have entered into my access database for the "Text9" and "List11" objects:

Option Compare Database

Const constErrNoError = 0
Const constQuote = """"

Public Function SearchRecordset(ctlText As Control, _
ctlList As Control, strBoundField As String) As Variant

    ' Search through a bound listbox, given text to
    ' find from a text box.
    ' Move the list box to the appropriate row.
    ' The listbox can have either a table or a dynaset
    ' (a query or an SQL statement) as its row source.
    ' In:
    ' ctlText: a reference to the text box you're typing into
    ' ctlList: a reference to the list box you're looking up in
    ' strBoundField: the name of the field in the underlying
    ' table in which you're looking for values.
    ' Out:
    ' Return value: either 0 (no error) or an error variant
    ' containing the error number.
    
    Dim rst As DAO.Recordset
    Dim varRetval As Variant
    Dim db As DAO.Database
    
    On Error GoTo HandleErr
    
    Set db = CurrentDb()
    Set rst = db.OpenRecordset(ctlList.RowSource, dbOpenDynaset)
    ' Use the .Text property, because you've not yet left the
    ' control. Its value (or its .Value property) isn't
    ' set until you leave the control.
    
    rst.FindFirst "[" & strBoundField & "] >= " & _
      constQuote & ctlText.Text & constQuote
    
    ' Find a match? Set the ListBox to
    ' the correctly value.
    If Not rst.NoMatch Then
        ctlList = rst(strBoundField)
    End If
    varRetval = constErrNoError

ExitHere:
    SearchRecordset = varRetval
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    Exit Function

HandleErr:
    varRetval = CVErr(Err)
    Resume ExitHere
End Function


Private Sub List11_AfterUpdate()
    UpdateSearch Me.Text9, Me.List11
End Sub



Private Sub Text9_Change()
    SearchRecordset Me.Text9, _
        Me.List11, "LastName"
End Sub


Private Sub Text9_Exit(Cancel As Integer)
    UpdateSearch Me.Text9, Me.List11
End Sub

Open in new window


Thanks

Glen
Avatar of PatHartman
PatHartman
Flag of United States of America image

The first thing you need to do when you add a control to a form is to change its Name property to something meaningful.  Text9 and List11 are not acceptable.

If you want to give the user control over how to search, let him enter the wildcards in the search box.  Otherwise, you can assume he will always enter the first few characters and you will add the * to the end of the string.

Change the RowSource of the list box to be a query that references the text box for its criteria.

Select ...
From ...
Where ClientName Like & "'" & Forms!yourform!lstClientName & "*'"
Order by ClientName;

In the AfterUpdate event of the text box, requery the list box.

Me.lstClientName.Requery
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GPSPOW

ASKER

Thanks