From: ats on
On Fri, 16 May 2008 13:22:53 -0400, BeastFish wrote:

> "ats(a)jbex" <allen(a)allenjones.NOSPAM.co.PLEASE.uk> wrote in message
> news:ocu7u0w92pt8.1tl2vq8v9ccla.dlg(a)40tude.net...
>> I have a combo box on a form that is poulated at runtime with a list of
>> years. When the form is populated, if the data has a year I set teh
>> combobox.text to display the year. If this then needs changing the
>> listindex is shown as -1. How can I iterate through the combobox items
>> until I find a matching entry and display that particular text?
>
>
>
> You can do this very quickly (even if there are thousands of items) using
> the SendMessage API on the combo box. Here's a brief example...
>
>
> (General)(Declarations)
>
> Private Declare Function SendMessageAny Lib "User32" Alias _
> "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
> ByVal wParam As Long, lParam As Any) As Long
> Private Const CB_FINDSTRING = &H14C ' First string that starts with...
> Private Const CB_FINDSTRINGEXACT = &H158 ' Exact string
>
>
> (code to find it)
>
> Dim SearchFor As String
> SearchFor = "1996"
>
> ' Send the FindStringExact message to
> ' the combobox, passing the string to find
> ' (returns the index if found, -1 if not found)
> Dim fIndex As Long
> fIndex = SendMessageAny(Combo1.hWnd, _
> CB_FINDSTRINGEXACT, _
> 0, _
> ByVal SearchFor)
>
> If fIndex > -1 Then
> ' Item found
> Combo1.ListIndex = fIndex
> Debug.Print "Found " & SearchFor _
> & " at " & fIndex
> Else
> ' Not found, so add it
> Combo1.AddItem SearchFor
> Combo1.ListIndex = Combo1.NewIndex
> Debug.Print "Added " & SearchFor _
> & " at " & CStr(Combo1.ListIndex)
> End If

Thanks, I will give that a go as well :-)
--
ats(a)jbex

Those who died are justified, for wearing the badge, they're the chosen
whites
You justify those that died by wearing the badge, they're the chosen whites

Rage Against The Machine - Killing In The Name