Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combo box help

Status
Not open for further replies.

tyedyejenn

Programmer
Jun 14, 2000
35
0
0
US
One of the features of a form I am designing is when the user types in the textbox part of a combo box the list box part drops down with the closest matching entry highlighted, so the user can hit enter and the entry will show in the textbox part of the form. I am unsure of how to make this happen. Any suggestions are greatly appreciated

Thanks in advance
Jenn
 
Not knowing exaclty how you have your screen set up.


If its for data entry you will probably do a "On Enter" set value of text box to that of the selected entry.

text1.caption=lookupbox.selection (something like this).

Jeremy
 
A Quick Search Combo Box

'Insert the following code to the module :

Dim strCombo As String
Const WM_SETREDRAW = &HB
Const KEY_A = 65
Const KEY_Z = 90
Declare Function SendMessage Lib "User32" (ByVal hWnd As Integer, ByVal wMsg As _
Integer, ByVal wParam As Integer, lParam As Any) As Long

'Insert the following code to your form:

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim x%
Dim strTemp$
Dim nRet&
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
'only look at letters A-Z
strTemp = Combo1.Text
If Len(strTemp) = 1 Then strCombo = strTemp
nRet&amp; = SendMessage(Combo1.hWnd, WM_SETREDRAW, False, 0&amp;)
For x = 0 To (Combo1.ListCount - 1)
If UCase((strTemp &amp; Mid$(Combo1.List(x), Len(strTemp) + 1))) = UCase(Combo1.List(x)) Then
Combo1.ListIndex = x
Combo1.Text = Combo1.List(x)
Combo1.SelStart = Len(strTemp)
Combo1.SelLength = Len(Combo1.Text) - (Len(strTemp))
strCombo = strCombo &amp; Mid$(strTemp, Len(strCombo) + 1)
Exit For
Else
If InStr(UCase(strTemp), UCase(strCombo)) Then
strCombo = strCombo &amp; Mid$(strTemp, Len(strCombo) + 1)
Combo1.Text = strCombo
Combo1.SelStart = Len(Combo1.Text)
Else
strCombo = strTemp
End If
End If
Next
nRet&amp; = SendMessage(Combo1.hWnd, WM_SETREDRAW, True, 0&amp;)
End If
End Sub

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
An alternative:

1. Start a new project and add 2 combo boxes, cboBox(0) and cboBox(1), - control array - and a timer, Timer1, to your form. Set the Enabled property of the timer to False, and set the Interval property to 500.

2. Add the following code to the form:

Option Explicit

Dim miControl As Integer

Private Sub Form_Load()
cboBox(0).AddItem &quot;Paul&quot;
cboBox(0).AddItem &quot;Simon&quot;
cboBox(0).AddItem &quot;James&quot;
cboBox(0).AddItem &quot;Stephen&quot;
cboBox(0).AddItem &quot;Steven&quot;
cboBox(0).AddItem &quot;John&quot;
cboBox(0).AddItem &quot;Jacob&quot;
cboBox(0).AddItem &quot;Pauline&quot;

cboBox(1).AddItem &quot;Paul&quot;
cboBox(1).AddItem &quot;Simon&quot;
cboBox(1).AddItem &quot;James&quot;
cboBox(1).AddItem &quot;Stephen&quot;
cboBox(1).AddItem &quot;Steven&quot;
cboBox(1).AddItem &quot;John&quot;
cboBox(1).AddItem &quot;Jacob&quot;
cboBox(1).AddItem &quot;Pauline&quot;

End Sub

Private Sub cboBox_Change(Index As Integer)
Timer1.Enabled = False
Timer1.Enabled = True
miControl = Index
End Sub

Private Sub Timer1_Timer()
Static bTimes As Integer

bTimes = bTimes + 1

If bTimes = 1 Then
Timer1.Interval = 1
Call Search_List(cboBox(miControl), cboBox(miControl).Text)
End If
If bTimes = 2 Then
Timer1.Enabled = False
Timer1.Interval = 500
bTimes = 0
End If

End Sub


3. Add a module to your project and place the following code in it:

Option Explicit

Public Function Search_List(cmbBox As ComboBox, sText As String) As Long
Dim i As Long, j As Long
Dim bStart As Boolean

On Error GoTo Ehandler

If cmbBox.ListIndex = -1 Then
j = 0
bStart = True
End If

If sText = cmbBox.List(j) Then
'found right away, lucky dog
Search_List = j
cmbBox.ListIndex = j
If cmbBox.Style = vbComboDropdown Then
cmbBox.Text = cmbBox.List(j)
Call Intelligent_Selection(cmbBox, sText)
End If

Exit Function
ElseIf bStart And cmbBox.Sorted And sText < cmbBox.List(j) Then
'sorted list and the searched for character is < first character
Search_List = j
cmbBox.ListIndex = j
If cmbBox.Style = vbComboDropdown Then
Call Intelligent_Selection(cmbBox, sText)
End If
Exit Function
End If

If cmbBox.Sorted Then
If sText > cmbBox.List(j) Or bStart Then
'step forward
For i = j + 1 To cmbBox.ListCount - 1
If sText = cmbBox.List(i) Then
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
cmbBox.Text = cmbBox.List(i)
Call Intelligent_Selection(cmbBox, sText)
End If

Exit Function
ElseIf sText < cmbBox.List(i) Then
'passed any suitable entry, move list but don't adjust text
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
Call Intelligent_Selection(cmbBox, sText)
End If
Exit Function
End If
Next i
'reached the end with no success
Search_List = -1
cmbBox.ListIndex = cmbBox.ListCount - 1

Exit Function
Else
'step backward
For i = j - 1 To 0 Step -1
If sText = cmbBox.List(i) Then
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
cmbBox.Text = cmbBox.List(i)
Call Intelligent_Selection(cmbBox, sText)
End If


Exit Function
ElseIf sText > cmbBox.List(i) Then
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
Call Intelligent_Selection(cmbBox, sText)
End If

Exit Function

End If
Next i
Search_List = -1
cmbBox.ListIndex = -1

Exit Function
End If
Else
'not sorted
For i = 0 To cmbBox.ListCount - 1
If sText = cmbBox.List(i) Then
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
cmbBox.Text = cmbBox.List(i)
Call Intelligent_Selection(cmbBox, sText)
End If

Exit Function
ElseIf Len(sText) < Len(cmbBox.List(i)) Then
If sText = Left$(cmbBox.List(i), Len(sText)) Then
Search_List = i
cmbBox.ListIndex = i
If cmbBox.Style = vbComboDropdown Then
cmbBox.Text = cmbBox.List(i)
Call Intelligent_Selection(cmbBox, sText)
End If
Exit Function
End If
End If

Next i
Search_List = -1
cmbBox.ListIndex = cmbBox.ListCount - 1

Exit Function
End If

Exit Function

Ehandler:
Search_List = -1
cmbBox.ListIndex = -1
Err.Raise Err.Number, &quot;modCmbBox.Search_List:&quot; &amp; Err.Source, Err.Description

End Function

Private Sub Intelligent_Selection(cmbBox As ComboBox, sText As String)
Dim i As Integer
Dim iLen As Integer
Dim sMatch As String
Dim bOnce As Boolean

On Error GoTo Ehandler

sMatch = cmbBox.Text

'check each character of sText to see if
' there is a matching character in cmbbox.text
i = 1

Do Until i = Len(sMatch) + 1 Or i = Len(sText) + 1
If Mid$(sText, i, 1) = Mid$(sMatch, i, 1) Then
i = i + 1
Else
Exit Do
End If
bOnce = True
Loop
i = i - 1

cmbBox.SelStart = i
cmbBox.SelLength = IIf(i > Len(sMatch), Len(sMatch), Len(sMatch) - i)

Exit Sub

Ehandler:
Err.Raise Err.Number, &quot;modCMBBox.Intelligent_Selection:&quot; &amp; Err.Source, Err.Description
End Sub


(This goes into a module so that it can be used from any form you want the type ahead to work on)

4. Run the project.
Simon
 
Simon,

I always get 'Pauline' not matter what I type. I didn't get any complie errors. Snaggs
tribesaddict@swbell.net
If a parsley farmer is sued, can they garnish his wages?
 
If you type capital S you should get. Just tested and it worked????
Simon
 
If you type capital S you should get Simon. Just tested and it worked????
Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top