Scan combo box for value

C

Chris

How can I loop through a combo box's list to determine if a particular value
is present?

Chris
 
B

Bruce M. Thompson

How can I loop through a combo box's list to determine if a particular value
is present?

Here's a quick example of searching through *all* the rows and columns of a
combo box:

'*****EXAMPLE START
Private Sub txtComboFind_AfterUpdate()
' Comments :
' Parameters:
' Created : 10/29/03 18:04 BMT
' Modified :
'
' --------------------------------------------------

On Error GoTo txtComboFind_AfterUpdate_ERR

Dim strCompare As String
Dim iListCount As Integer
Dim iRow As Integer 'The Row
Dim iColumn As Integer 'The Column

'The search value
strCompare = Me.txtComboFind.Value

With Me.cboNames
'Get count of "data" rows
iListCount = .ListCount - 1 + Abs(.ColumnHeads)
'Cycle through Rows
For iRow = Abs(.ColumnHeads) To iListCount
'Cycle through Columns
For iColumn = 0 To .ColumnCount - 1
'Compare values in combo's list to string variable
If .Column(iColumn, iRow) = strCompare Then
Me.txtFindStat.Value = "Yes"
'Select first Row containing value and then
' exit sub
.Value = .ItemData(iRow)
Exit Sub
Else
Me.txtFindStat.Value = "No"
End If
Next iColumn
Next iRow
End With

txtComboFind_AfterUpdate_EXIT:
Exit Sub

txtComboFind_AfterUpdate_ERR:
MsgBox "Error " & Err.Number & _
" occurred in txtComboFind_AfterUpdate: " & Err.Description
Resume txtComboFind_AfterUpdate_EXIT

End Sub
'*****EXAMPLE END

You can modify the code to search only a specific column by replacing "iColumn"
in ".Column(iColumn, iRow)" with the column number and removing the lines ...

'Cycle through Columns
For iColumn = 0 To .ColumnCount - 1

.... and ...

Next iColumn

.... along with any other lines that don't suit your purpose. Obviously, all
control names would need to be changed to match those in your project.
Hopefully, this example will help you understand the technique I used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Combo box 0
Weekly Report 2
VBA office 365 on windows 10 0
Combo or list box 0
Automation of scanned file storage based on content 0
Combo box returning wrong value 2
Update Combo Box 4
OnChange combo box 2

Top