Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Partial Fild Search on Listbox

Posted on 2011-02-11
13
Medium Priority
?
311 Views
Last Modified: 2012-05-11
Hi Experts,

MS Access 2007, running on Win 2003 Server.

We have a listbox on a form for selecting records to display in that form - it contains a list of customer names. I know that hitting the first letter in the listbox will take me to the first record in the list starting with that letter but the list is getting quite long -  is there a way that a user could input the first 3 characters into a text box, then for the listbox to go the first record matching (or near matching) those 3 characters?

Example:
User enters "sma" in text box, listbox contains "small" and "smart", so presented record would be "small"
User enters "smo" but there is no exact match, so record returned should be either previous or next best match

Any thoughts gratefully received.
0
Comment
Question by:redpoppy
[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
  • 7
  • 6
13 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34873581
use the change event of the textbox

private sub text0_change()
dim str as string, sql
str=str & me.text0.text

sql="select f1,f2 from table where f1 like '" & str & "*'"

me.listbox.rowsource=sql

end sub
0
 

Author Comment

by:redpoppy
ID: 34877965
Hi capricorn1 - thanks very much for such a speedy reply - I'll try it out over the weekend and get back to you.
0
 

Author Comment

by:redpoppy
ID: 34889023
Hi capricorn1 - thanks for replying, I've now had chance to have a look at your suggestion.

What I actually want to do is to take the 3 characters entered, do the look up in the list box, then if there is a match make it the selected record  in the listbox i.e. I don't want to lose all the other records in the listbox by changing the rowsource  - is that possible? Thanks again for your help.
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34889067
you are not going to lose the records in the listbox, you are just narrowing your search.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34889115
post the rowsource of the listbox
0
 

Author Comment

by:redpoppy
ID: 34889148
sorry, I don't understand - if I'm replacing the value of the listbox rowsource (from your original code), then how are the other records going to be displayed?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34889177
<if I'm replacing the value of the listbox rowsource (from your original code), then how are the other records going to be displayed? >

by setting back the the rowsource to the original rowsource without the filter.
0
 

Author Comment

by:redpoppy
ID: 34889189
The rowsource of the listbox can be one of several queries - the first 2 columns are always "cust_name" and "cust_id"
0
 

Author Comment

by:redpoppy
ID: 34889226
"by setting back the the rowsource to the original rowsource without the filter." OK, I see. That won't work for what I need to do - all the records in the listbox need to be displayed and I want to use the 3-chr search to go to the nearest record in the listbox, i.e. making it the current selected/displayed record
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34889287
what is the bound column of the listbox? what is the name of the listbox?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 34889305

try this codes

private Sub text0_change()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Set db = CurrentDb
    strSQL = "select cust_name, cust_id " & _
             "from tableName where cust_name like '" & _
             me.text0.text & "*'"
        
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

    If rs.recordCount > 0 Then
        rs.MoveFirst
        Me.listboxName = rs!cust_id
    End If

    rs.Close
    Set rs = Nothing
    Set db = Nothing
end sub

Open in new window

0
 

Author Comment

by:redpoppy
ID: 34889620
OK - I think I see what you're getting at - to do a look-up on cust_name using the 3 chr input, then setting that cust_id as the current record in the list box? (cust_id is the bound field)
0
 

Author Closing Comment

by:redpoppy
ID: 34901499
I based my solution on the code supplied by capricorn1 - I used the rowsource from the listbox as my dataset and did a look-up on it to find the record, then set this as the current record in the listbox. Thanks for your help, much appreciated
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

705 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