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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Box Problems

Status
Not open for further replies.

Compuboy

Programmer
Jun 8, 2004
38
US
I have a combo box in a form and I want to search it for certain strings. Right now I'm only doing

If Me.Combo0.text = "hello" then whatever...

But that only tests the text that is currently selected. How would I test every entry in the combo box to see if any entry was "hello".

If I can't test everything in the combo box how can I check every entry in the column of the table that the combo box is based on.

Thanks. You guys rock.
 
You need to tell us why you want to do what you want to do. The recordset source of a combobox is, of course, a recordset. You can set the criteria to the test value of "hello" or any other OR You can search the recordset for the value. It depends on what you want to do with it once you have found it.

Rollie
 
You could try looping through the Combobox ItemData array. Inside of that array are all of entries contained in the combobox.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I just want to know how to search all the entries in the combobox to see if any entries match a certain string (e.g. "hello"). I can take it from there I think.

I just don't know what functions comboboxes have and I know how to really write VB code (I only write C++). The ItemData array sounds like it would work...can you give me an example code or something? Thanks a mill.
 
In your example code just put IF _____ then whatever... The part after "then" don't worry about.
 
How are ya Compuboy . . . . .

Or you could try something like this:
Code:
[blue]Public Function TextExist(txtSearch As String) As Boolean
   Dim n As Integer, ctl As ComboBox
   
   Set ctl = Me![purple][b]YourComboBoxName?[/b][/purple]
   
   For n = 0 To ctl.ListCount - 1
      If ctl.Column(1, n) = txtSearch Then
         TextExist = True
         Exit For
      End If
   Next

End Function[/blue]

Calvin.gif
See Ya! . . . . . .
 
There is a sample in the help files of looping through the ItemData array.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
hmmm...I don't know why the code doesn't work..
 
Compuboy . . . . .

Post the code you used!

Calvin.gif
See Ya! . . . . . .
 
Public Function TextExist(txtSearch As String) As Boolean
Dim n As Integer, ctl As ComboBox

Set ctl = Me.Combo0

For n = 0 To ctl.ListCount - 1
If ctl.Column(1, n) = txtSearch Then
TextExist = True
Exit For
Else
TextExist = False
End If
Next

End Function
 
OK Compuboy . . . . .

Change [blue]ctl[purple].[/purple]ListCount - 1[/blue] to:
[blue]ctl[purple]![/purple]ListCount - 1[/blue]

There will be a referencing problem if you did not put the code in a form module. So that it works anywhere:

Change [blue]Set ctl = Me.Combo0[/blue] to
[blue]Set ctl = Forms![purple]YourFormName[/purple]!Combo0[/blue]

Also you do not need:
[blue]Else
TextExist = False[/blue]
The function defaults to false when you call it.

Calvin.gif
See Ya! . . . . . .
 
wow, youre good..except.

it doesn't work when I use ctl!ListCount - 1 instead of ctl.ListCount - 1.

And it does run when I have ctl.ListCount-1 and it goes through every item becuase I tried MsgBoxing each time, but for some reason its just not returning true when the string matches.

Any ideas? Thanks.
 
I got it actually...I just had to change ctl.Column(1, n) to ctl.Column(0, n)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top