Textbox SELECTS Listbox Entry

Randomblink

The Irreverent Reverend
Local time
Today, 08:15
Joined
Jul 23, 2001
Messages
279
Ok...
I have seen this done and am HIGHLY irritated that I can't find it on this board... Heck, I have DONE this, and I can't remember...

What I want is:

I have a textbox. Unbound.
I have a listbox. RowSource = SQL Code I wrote

I want the listbox to JUMP to the first entry that matches the current textbox text...

For instance...
If my listbox is of all 50 states...
and I Type the Letter 'O' into the textbox...
I want the listbox to jump to AND select Ohio...

IF the next character I type after 'O' is 'K' then I want the listbox to jump PAST Ohio and Select Oklahoma...

I do NOT want to readjust the SQL for the listbox...
I want ALL the entries to still be there...
So the user could change their mind and mouse-click another state instead at this point... or they could keep the listbox selected at Oklahoma...

I HOPE I have made this clear...
EVERY post that does something similar to this actually requeries the listbox using a newly adjust SQL setup...

I just want to JUMP the listbox to THE CLOSEST entry... Not removing ANY data from it, just SELECTING data that is closest to the current entry in the textbox...

Can you help? If so it would be greatly appreciated...
Thanks...
 
at a very basic level this works when the values typed appear in the list, when they don't the first item is selected because the function returns 0.

Code:
Private Sub txtFind_Change()
List0.Selected(FoundNo(Len(txtFind.Text))) = True
End Sub


Private Function FoundNo(bytLength As Byte) As Byte
Dim i As Byte

For i = 0 To List0.ListCount - 1
    If Mid(List0.Column(1, i), 1, bytLength) = txtFind.Text Then
        FoundNo = i
        Exit For
    End If
Next i
End Function
 
FOLLOW UP: Completed COde / Reusable Code

Fornation...
Thank you...
I took what you gave me...
Modified it for reusability...
Commented the Living Hell out of it...
And am dropping it here for anyone who could use it...

Code:
Public Function ListSelector(TextToSearch As String, lst As ListBox)
On Error GoTo Err_Selector
'*****************************************************************
'*  Argument(required/optional)     Basic Description             *
'*******************************************************************
'   TextToSearch (required)         Text that we are going to find a Match inside of the Listbox
'   lst(required)                   This is the LISTBOX we are searching through in looking for TextToSearch
'*****************************************************************
'* Variables                    Variable Definitions              *
'*******************************************************************
Dim iColumn As Integer      '   Variable used to Run through the various Columns in the Listbox
Dim iRow As Integer         '   Variable used to Run through the various Rows in the Listbox
Dim iWord As Integer        '   Variable used to Run through the various Words in the Cells of the Listbox
Dim sCell As String         '   Variable used to point to the Cell being checked
Dim iCellSize As Integer    '   Variable used to Size the Cell Contents being checked
Dim iWordDig As Integer     '   Variable used to Choose how far in to the Cell Contents we are going to dig
Dim iTextSize As Integer    '   Variable used to determine how many characters are in the Text to Search
'*****************************************************************

' The First thing we need to do is find out how many 'CHARACTERS' there are in the TextToSearch Variable.
' For Example:
' The Word "HOSPITAL" has 8 Letters... or CHARACTERS in it...
' SO, if TextToSearch contains the string 'HOSPITAL' then Len(TextToSearch) will = 8
iTextSize = Len(TextToSearch)
' Then, if our TextToSearch is Empty (or it EQUALS ZERO)...
' Then we are finished and we can EXIT this Function
If iTextSize = 0 Then GoTo Exit_Selector
' Now we want to create a counter.
' First we need to Find out How many COLUMNS (not to be confused with ROWS) that this Listbox has.
' The lst.ColumnCount will count ALL the Columns in this ListBox
' So, Now we create a Counter called iColumn.
' This Counter will count from 1 all the way up to the number of COLUMNS we have in this Listbox.
For iColumn = 0 To lst.ColumnCount - 1
    ' Again, we do the same thing...
    ' Here we create a Counter called iRow.
    ' This Counter will count from 0(ZERO) to the number of ROWS we have in this Listbox MINUS 1
    For iRow = 0 To lst.ListCount - 1
        ' Ok, we are Counting now.
        ' If the counter JUST started, then iColumn = 1 and iRow = 1
        ' Now, we are going to create a pointer.
        ' This pointer will point to a SPECIFIC CELL in our Listbox.
        ' In our example above, if iColumn =1 and iRow = 1
        ' Then the SPECIFIC CELL we are pointing at would be in Column 1, Row 1
        ' So, sCell Equals the Cell that is located at: Column:iColumn / Row:iRow
        sCell = lst.Column(iColumn, iRow)
        ' Now that we have pointed to a Cell, we need to see how many Characters are in this Cell.
        iCellSize = Len(sCell)
        ' With the Size of the Cell now figured out, we need to know how far INTO this cell we want to look.
        ' If the Entry in this Cell is 10 Characters long... and our iTextSize is only 2...
        ' (Remember: iTextSize is a number that is equal to how many Characters are in our TextToSearch)
        ' I will explain in a moment WHY we Subtract iCellSize from iTextSize... For now just understand:
        iWordDig = iCellSize - iTextSize
        ' Ok, again, I will explain in a moment, but we need to make sure that iWordDig is equal to 1 or more.
        ' So, we have an If/Then statement.
        ' IF iWordDig is Less than OR Equal to ZERO, then we need to bump it up to equal the number 1
        If iWordDig <= 0 Then iWordDig = 1
        ' ANOTHER Counter.
        ' This time, we are counting from 1 to WHATEVER NUMBER WE CAME UP WITH FOR iWordDig... see above if you have a question.
        For iWord = 1 To iWordDig
            ' OK... Now I explain.
            ' See, we are going to take apart each Cell and dig piece by piece for a Match.
            ' If you will notice the MID statement?
            ' The Correct Definition for how MID works is this...
            ' Mid(String, Start As Long,[Length])
            ' What does this mean?
            ' Well, when you use Mid... you tell it two to three things.
            ' First, you give Mid a String.
            ' We are giving Mid the sCell, remember from above? That is the Entry in the Cell?
            ' Ok, we give Mid the sCell... what next? We next we tell it WHAT CHARACTER NUMBER...
            ' If you take sCell, and you number each CHARACTER in it, then "Start As Long" is the Character we will start with.
            ' SO, if sCell is equal to "HOSPITAL" and we tell it to "Start As Long" with a 4...
            ' Then that Means we want to start at CHARACTER NUMBER 4 INSIDE HOSPITAL... which is the Letter "P"
            ' The Last Part of Mid is [Length]. This is actually an Optional point.
            ' We COULD use Mid without telling it the [Length]. However, We WANT to tell it the Length...
            ' What IS the Length?
            ' Ok...
            ' If we do this like we did the above, lets say we are at the First Time through this counter
            ' That would Mean iWord = 1
            ' THIS MEANS:
            ' 1) We are checking the Contents of sCell
            ' 2) We are Starting at CHARACTER iWord... or 0(ZERO) for this Example...
            ' 3) AND we are Grabbing a number of CHARACTERS equal to iTextSize
            ' THIS is they we Subtracted iCellSize from iTextSize...
            ' When we dig through through sCell, if it is 5 CHARACTERS Long...
            ' TWO THINGS CAN HAPPEN:
            ' 1) FIRST THING:
            ' the TextToSearch COULD be 6 CHARACTERS Long...
            ' this would mean that iWordDig would equal a negative number, and therefore...
            ' Our iWord counter would count from 1 to a negative number which is NOT what we want...
            ' Which means we would NOT EVEN CHECK sCell AT ALL...
            ' 2) SECOND THING:
            ' the TextToSearch COULD be JUST as Long as sCell
            ' and the SAME thing would happen... we would SKIP it cause iWordDig would equal 0(ZERO)
            ' So, we have to check it first.
            
            ' Ok, now that we have checked sCell... at Character iWord...
            ' And we have Grabbed iTextSize many CHARACTERS...
            ' We now check to see if it equals TextToSearch...
            If Mid(sCell, iWord, iTextSize) = TextToSearch Then
                ' If it does...? GREAT! WE HAVE FOUND A MATCH!
                ' IF we found a Match, we will Grab our Listbox with the lst
                ' THEN we HIGHLIGHT a Row INSIDE the Listbox with Selected
                ' The ROW we Highlight is Row number iRow
                ' After that, we END the Function.
                lst.Selected(iRow) = True: GoTo Exit_Selector
            ' IF what we grabbed with Mid matched, then our IF/THEN happened...
            ' Now if we HADN'T used the (Goto Exit_Selector) then we would NEED "End If"
            ' As it is, we are using End If just in case something goes wrong.
            End If
        ' Ok, the Command (Next iWord) means...
        ' Run the Counter, but BUMP iWord up ONE MORE NUMBER...
        ' AND Lets go up and start Executing Code at the For iWord = blah blah blah
        ' So we will NOT Execute (Next iRow) until we reach the END of iWord...
        Next iWord
    ' Same thing for iRow as for iWord...
    Next iRow
' AND SAME THING FOR iColumn...
Next iColumn

' Ok, we create a SubRoutine in our Function and we call it: Exit_Selector
Exit_Selector:
' We assign ONE Command to it... (Exit Function)
' When this SubRoutine Happens... This Function is DONE
Exit Function

' Now we need another SubRoutine in our Function as we call IT: Err_Selector
Err_Selector:
' We assign TWO Commands to IT...
' errMessage Err is a Custom Built Function I use to handle Errors...
' If you want you can use the following...
' MsgBox Err.Description
errMessage Err
' NOW we Resume on to Exit_Selector which ends the Function...
' WHY? Because we had an error and there is no reason to continue.
Resume Exit_Selector
' If you have ANY QUESTIONS... Feel free to email me at: [email]randomblink@yahoo.com[/email]
' Started by Fornatian @ [url]www.access-programmers.co.uk[/url]
' See the thread: [url]http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=56992[/url]
' Finished by randomblink...
End Function

Thanks again...
 
Last edited:
Very cool. I tried out your code and have just one thing to modify:

For iRow = 0 To lst.ListCount - 1
instead of:
For iRow = 1 To lst.ListCount - 1

Try typing something in, and then backspacing and starting something that is in the first row.
 
Changed it...

Question for ya...
WHY?
I mean I can understand that it works, but.. I can't wrap my head around why?

Of course, I did go home after work without touching the computer... watched TV, ate, went to bed early and am JUST NOW touching the computer... so it has been too long since I last 'programmed'... chuckle...

But seriously, I can't seem to understand... why not count from 0 to ListCount? or 0 to ListCount +1?

Any help would be appreciated...
Thanks...

(I am trying to model my code after something I read about... where you comment, THEN add code... Makes it easier for NEWBS... however, the sad thing is, there are some things that I KNOW work... But I couldn't explain why for the life of me... ugh!)

Oh, and thanks again...
 
cool code random, i understand why iRow should = 0 and not 1, rows in a listbox start at Row 0 not one, so if you search return and start typing the listbox searches from the 2nd row onwards.

I think. :D
 
Yup, absolutely right.

Just like you start your column reference at zero, the rows are referenced just the same. It appears to work at first, because it will select the first row by default, but start typing in an entry that is in the third row, backspace and try entering something in the first row. It won't be able to see it because the reference starts one row ahead.

Great code, though.
 

Users who are viewing this thread

Back
Top Bottom