Avatar of redpoppy
redpoppy

asked on 

Partial Fild Search on Listbox

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.
Microsoft Access

Avatar of undefined
Last Comment
redpoppy
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

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
Avatar of redpoppy
redpoppy

ASKER

Hi capricorn1 - thanks very much for such a speedy reply - I'll try it out over the weekend and get back to you.
Avatar of redpoppy
redpoppy

ASKER

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.
you are not going to lose the records in the listbox, you are just narrowing your search.
post the rowsource of the listbox
Avatar of redpoppy
redpoppy

ASKER

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?
<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.
Avatar of redpoppy
redpoppy

ASKER

The rowsource of the listbox can be one of several queries - the first 2 columns are always "cust_name" and "cust_id"
Avatar of redpoppy
redpoppy

ASKER

"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
what is the bound column of the listbox? what is the name of the listbox?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of redpoppy
redpoppy

ASKER

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)
Avatar of redpoppy
redpoppy

ASKER

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
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo