Solved

Creating a  text box to search a list box

Posted on 2014-10-17
5
303 Views
Last Modified: 2014-10-18
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
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 37

Expert Comment

by:PatHartman
ID: 40387421
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
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 350 total points
ID: 40387429
change this codes

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


with

Private Sub Text9_Change()
    SearchRecordset Me.Text9.Text, _
        Me.List11, "LastName"
End Sub
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 75 total points
ID: 40387500
To explain a little about Rey's recommendation.

When you execute code within the Change event of a control, you must use the .Text property of the control rather than the default '.Value' property.  The Change event fires every time you make a change in the control, but the actual value of those changes is not visible until the controls AfterUpdate event.

You can test this by doing something like:

Private Sub Text9_Change

    debug.print "Value:";me.text9;"   Text:";me.text9.text

End Sub

You will see that the Value property contains the previous value (the last time you hit enter or exited that control), while the Text property changes as you type.
0
 
LVL 50

Assisted Solution

by:Gustav Brock
Gustav Brock earned 75 total points
ID: 40388078
0
 

Author Closing Comment

by:GPSPOW
ID: 40388835
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question