473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Search in Listbox via Textbox

Hello,

Just for anyone information, there is a similar title "Search in Listbox" but it is via Combo Box. In case, anyone need it, I put a link to here.

Please let me know if I break any rules by posting a link so then, I will remove it.

Okay, here is my question. If you ever refer to the link, I am almost doing the same thing. Just that, I am doing a textbox and a combo box to search the listed data in the listbox.

The listbox consist of multiple column (PartID, Category, Engine, Model, Brand [Source from a Query - EX]). When user like to search, he/she can choose the combo-box (for which column to search) and type in the textbox, and finally press the Search button. Then the listbox will locate the closest/exact match and select/highlight it. A note, not filter but the listbox will Go To The Specific Data and Select/Highlight It.

My Listbox RowSource:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW PartID, Category, Engine, Model, Brand FROM EX ORDER BY PartID; 
Code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRun2_Click()
  2.  
  3. Dim i As Integer
  4. Dim A As Integer
  5. Dim txtSelect1 As String
  6. Dim response As Integer
  7.  
  8. For i = 0 To listPart.ListCount - 1
  9.  
  10.     'To determine Selected-Combo-Box and the Search Column of the listbox
  11.     If qSelect1 = "PartID" Then
  12.         A = 0
  13.  
  14.         ElseIf qSelect1 = "Category" Then
  15.                A = 1
  16.  
  17.                 ElseIf qSelect1 = "Engine" Then
  18.                    A = 2
  19.  
  20.                     ElseIf qSelect1 = "Model" Then
  21.                         A = 3
  22.  
  23.                         ElseIf qSelect1 = "Brand" Then
  24.                             A = 4
  25.     End If
  26.  
  27.     listPart.Selected(i) = False
  28.  
  29.     'Try to find the closest match
  30.     txtSelect1 = "" & txtSelect1 & " * "
  31.  
  32.     'After it determine which Column to search, "A" represent it
  33.     If listPart.Column(A, i) Like txtSelect1 Then
  34.  
  35.     listPart.Selected(i) = True
  36.  
  37.     listPart.SetFocus
  38.  
  39.     'After the data had been search & selected, it will auto Order By 
  40.     response = listOrderBy(qSelect1, "ASC")
  41.  
  42.     End If
  43.  
  44. Next
  45.  
  46. End Sub
The problem is I'm not really sure what happened. No highlighted search result after press "Search". No Error.

If you refer to the Top-Link (Search in Listbox via Combo), I try to replicate the way it code. Still, not working.

Hope someone can help. I don't mind a new way to code this, just let me know. Thanks in advance.
Jul 29 '09 #1
9 10635
ChipR
1,287 Expert 1GB
Line 30 should reference the value of the text box on the form. No guarantees about the rest of the code, though.
Jul 29 '09 #2
txtSelect1 is the Textbox's name

Without success, I try the conventional way by removing all the wildcard search. Standard/Exact search only.

Expand|Select|Wrap|Line Numbers
  1. listPart.Selected(I) = False
  2.  
  3.     'Able to find the closest match. I remove this line by commenting it.
  4.     'txtSelect1 = "" & txtSelect & " * "
  5.  
  6.     'After it determine which Column to search, "A" represent it
  7.     'Without wildcard search, I used equal = sign
  8.     If listPart.Column(0, I) = txtSelect Then
  9.  
  10.     listPart.Selected(I) = True
  11.  
  12.     listPart.SetFocus
It works perfectly fine, if user type the exact search-text. Is there any other way, I can add in a wildcard search. Eg. 1 * or Ha *
Jul 30 '09 #3
OldBirdman
675 512MB
I think you want to search for 'Ha*' or '1*', and not 'Ha * ' or '1 * '. I would not think you want entries beginning with 'Ha ' then some characters, then a trailing blank. Try line 30
Expand|Select|Wrap|Line Numbers
  1. txtSelect1 = txtSelect1 & "*" 
This will give all entries (in the appropriate column) beginning with 'Ha'. '*Ha*" will give all entries containing the string 'Ha' and so would find 'HeeHaw' as well as 'Hawfinch'.

Lines 11-25 should be moved outside the loop. The value of A will not change during the loop.
Jul 30 '09 #4
ChipR
1,287 Expert 1GB
Note that
txtSelect1 = txtSelect1 & "*"
is still wrong, and probably due to an error in copying the original code. You wouldn't set an empty string variable equal to an empty string variable. I would rename txtSelect1 to strSelect to avoid those needless errors.
Jul 30 '09 #5
OldBirdman
675 512MB
txtSelect1 = txtSelect1 & "*"
does not set an empty string to an empty string. If txtSelect is empty, this would highlight all entries, which is probably not what the user wants. Either this routine can do as the user enters (right or wrong), or it can do some logical checking for blank/empty textbox, trailing blanks, or whatever else might be deemed incorrect. That may be in the actual code, simplified here for posting.

I would add a string variable strSelect to this subroutine, and then
Expand|Select|Wrap|Line Numbers
  1. strSelect1 = txtSelect1 & "*"
replacing txtSelect1 with strSelect1 as appropriate in the code. But I framed my answer in the framework of the original code, not the code I would have written.

I would think that getting no results with wildcards, but getting results with an exact match would be because of the blanks surrounding the '*' in the compare. That is the question I was answering.
Jul 30 '09 #6
ChipR
1,287 Expert 1GB
I think you missed my point. txtSelect1 is a string variable declared at the beginning of the sub. txtSelect is the control on the form, judging by line 4 of post 3. If you renamed txtSelect1 to strSelect1, then that line of code would tell you that txtSelect1 was undeclared.
Jul 30 '09 #7
Thanks a lot, people. Again, thanks for your help. It works now but ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRun2_Click()
  2.  
  3. Dim I As Integer
  4. Dim A As Integer
  5. Dim strSelect As String
  6. 'Dim response As Integer
  7.  
  8. 'To determine Selected-Combo-Box and the Search Column of the listbox
  9. If qSelect1 = "PartID" Then
  10.         A = 0
  11.  
  12.         ElseIf qSelect1 = "Category" Then
  13.         A = 1
  14.  
  15.                 ElseIf qSelect1 = "Engine" Then
  16.                 A = 2
  17.  
  18.                     ElseIf qSelect1 = "Model" Then
  19.                     A = 3
  20.  
  21.                         ElseIf qSelect1 = "Brand" Then
  22.                         A = 4
  23. End If
  24.  
  25. For I = 0 To listPart.ListCount - 1
  26.  
  27.     listPart.Selected(I) = False
  28.  
  29.     'Able to find the closest match
  30.     strSelect = txtSelect1 & "*"
  31.  
  32.     'After it determine which Column to search, "A" represent it
  33.     If listPart.Column(A, I) Like strSelect Then
  34.  
  35.     listPart.Selected(I) = True
  36.  
  37.     listPart.SetFocus
  38.  
  39.     'After the data had been search & selected, it will auto Order By
  40.     'response = listOrderBy(qSelect1, "ASC")
  41.  
  42.     End If
  43.  
  44. Next
  45.  
  46. End Sub
but ... the search result highlighted the last record of search-text.

For an example, I want to search for anything in Column A that has B1 in front.

Search result: B12, B13, B133, B150, B190

It will highlight B190 rather than the first closest-match, B12

Anyone encounter this issue before?
Jul 31 '09 #8
ChipR
1,287 Expert 1GB
That is the result of your logic. In the FOR loop, you step through each item. In your example, B12 would be selected, then B13, then B133, etc., and finally B190. You can solve this by inserting "exit sub" at line 38 to stop when the first match is found.
Aug 3 '09 #9
Thanks a lot, ChipR. Really appreciate all your help.
Aug 5 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: damjanu | last post by:
Dear All; I have 3 issues 1) On some of my tables I have unique indexes, so that for example no two phone numbers can be the same. When user enters phone number that already exists, a scary...
0
by: Colleyville Alan | last post by:
I have an app that looks up an id number and when a button is clicked, inputs the id # into a query. After running the query, I click a second button which grabs the client name rather than the id...
3
by: Colleyville Alan | last post by:
I have a incremental search box that has been working fine for a couple of months but is now acting up. This search box is from the cd that comes with Getz's book, so I did not write it and have...
2
by: John R. | last post by:
I want to have a listbox that shows a checkbox and a textbox. I created a user control that has a checkbox and a textbox in it and have been trying to add it to a listbox but I can't get it to...
1
by: Hrvoje Voda | last post by:
I'm going through the listbox items with cursor. I would like to put the selected listbox text into a textbox. What method should I use? Hrcko
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
15
by: allansiu823 | last post by:
Hi I am new to MS Access and VBA and have been bothered by this problem for a looooong time. I have this listbox containing all the records (~1000) in a form and I want to be able to type something...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
10
by: Eugenio | last post by:
Hi there, I'm returning to this forum for the second time and I would like to say thanks for the great help provided. I've encountered a new problem now and hope that you will be able to help me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.