Home All Groups Group Topic Archive Search About

Check Text is in a combo box?

Author
23 Sep 2005 3:03 PM
Damon
Hi,

I have a combo box which has three items in it i.e. Finance, ICT, Admin.  I
want the user to be able to add new items to the combo box.  When they type
in a new entry i.e. Housing I want to be able to check that Housing isn't
already in my combo box list and if it isn't add it.

Thanks in advance for any help

Damon

Author
23 Sep 2005 3:16 PM
Ken Halter
"Damon" <nonse***@nononsense.com> wrote in message
news:4PUYe.1579$NO2.830@newsfe4-win.ntli.net...
> Hi,
>
> I have a combo box which has three items in it i.e. Finance, ICT, Admin.
> I want the user to be able to add new items to the combo box.  When they
> type in a new entry i.e. Housing I want to be able to check that Housing
> isn't already in my combo box list and if it isn't add it.
>
> Thanks in advance for any help
>
> Damon

One problem is deciding when to actually add the new item <g>. This code
will search the list if the user hits Enter after typing in the combobox.
You can move the code to LostFocus or a function or..... where ever...

Needs a new project and a combobox.
'===============
Option Explicit

Private Declare Function SendMessage _
                Lib "user32" _
                Alias "SendMessageA" (ByVal hwnd As Long, _
                                      ByVal wMsg As Long, _
                                      ByVal wParam As Long, _
                                      lParam As Any) As Long

Private Sub Form_Load()
   With Combo1
      .Clear
      .AddItem "Apple"
      .AddItem "Orange"
      .AddItem "Cat"
      .AddItem "Dog"
      .ListIndex = 0
   End With
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
   Const CB_FINDSTRING = &H14C
   Const CB_ERR = (-1)
   Dim lResult As Long

   If KeyAscii = vbKeyReturn Then
      KeyAscii = 0 'prevent beep
      lResult = SendMessage(Combo1.hwnd, CB_FINDSTRING _
         , -1, ByVal Combo1.Text)

      If lResult = CB_ERR Then 'no match found
         Debug.Print "Adding '" & Combo1.Text & "' to the list"
         Combo1.AddItem Combo1.Text
      End If

   End If

End Sub
'===============

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
23 Sep 2005 3:20 PM
Saga
A quick and dirty:

   Dim strText as string
   Dim i as Integer

   strText = Ucase(Combo1.text)

   for i = 0 to Combo1.ListCount -1
      if strText = UCase(Combo1.List(i)) then
        'Element already in list.
        Exit For
      End If
   next i

   if i = Combo1.ListCount then
      'Element not in list do whatever.
      Combo1.AddItem strText
   else
      'Element in list, take necessary action.
   end if

This works for combo lists with few items. There are more
efficient search algorithms (using API I believe) for Combos
that have many elements.

Good luck!
Saga

Show quoteHide quote
"Damon" <nonse***@nononsense.com> wrote in message
news:4PUYe.1579$NO2.830@newsfe4-win.ntli.net...
> Hi,
>
> I have a combo box which has three items in it i.e. Finance, ICT,
> Admin.  I want the user to be able to add new items to the combo box.
> When they type in a new entry i.e. Housing I want to be able to check
> that Housing isn't already in my combo box list and if it isn't add
> it.
>
> Thanks in advance for any help
>
> Damon
>