How to check for duplicates in Combo Box
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 'Controls - How to check for duplicates in Combo Box 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 Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long Const CB_SHOWDROPDOWN = &H14F Const CB_FINDSTRINGEXACT = &H158 Private Sub Command1_Click() With Combo1 If Not CheckDuplicates(.hWnd, .Text) Then .AddItem .Text End If SendMessage .hWnd, CB_SHOWDROPDOWN, True, ByVal 0& End With End Sub Private Sub Form_Load() 'Put some items in the combobox With Combo1 .AddItem "Sunday" .AddItem "Monday" .AddItem "Tuesday" .AddItem "Wednesday" .AddItem "Thursday" .AddItem "Friday" .AddItem "Saturday" 'Provide the default value .Text = .List(0) 'Open the dropdown list SendMessage .hWnd, CB_SHOWDROPDOWN, True, ByVal 0& End With End Sub Private Function CheckDuplicates(chwnd As Long, StringText As String) As Boolean CheckDuplicates = SendMessageByString(chwnd, CB_FINDSTRINGEXACT, -1, ByVal StringText) > -1 End Function |