By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,191 Members | 2,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,191 IT Pros & Developers. It's quick & easy.

Populate combo box with only unique values

P: 8
Hello,

I am trying to populate a combo box with only unique values. Currently I am using an access database and VB6 Enterprise. The program populates the combo box fine however, there are several repeated values in this column. What i would like to do is just show one instance of that particular record.

For example:

if the column has the values: Accounts Payable, Process Control, Shipping, Accounts Payable

i would like the combo box just to list: Accounts Payable, Process Control, Shipping


I have tried several attempts and comparing each record set as they are listed, but that seems to throw me into an infinite loop and the program will crash. Eventually i would like it to work as follows, however if there is another way I am open for suggestions.

While Not rs.EOF
if rs!Column_Name <> "" Then
strData = rs!Column_Name (strData is a string)
Call CheckRepeat (will return a boolean Unique)
if Unique = True Then
Combo1.Additem (strData)
end if
rs.MoveNext
end if
wend

Thanks!!
Nov 2 '06 #1
Share this Question
Share on Google+
5 Replies


sashi
Expert 100+
P: 1,754
Hi there,

Use the DISTINCT keyword, all you need is a little SQL knowledge. Take a look at below sample code segment. Good luck & take care.

Expand|Select|Wrap|Line Numbers
  1.   Select DISTINCT fldName from tblName
  2.  
Hello,

I am trying to populate a combo box with only unique values. Currently I am using an access database and VB6 Enterprise. The program populates the combo box fine however, there are several repeated values in this column. What i would like to do is just show one instance of that particular record.

For example:

if the column has the values: Accounts Payable, Process Control, Shipping, Accounts Payable

i would like the combo box just to list: Accounts Payable, Process Control, Shipping


I have tried several attempts and comparing each record set as they are listed, but that seems to throw me into an infinite loop and the program will crash. Eventually i would like it to work as follows, however if there is another way I am open for suggestions.

While Not rs.EOF
if rs!Column_Name <> "" Then
strData = rs!Column_Name (strData is a string)
Call CheckRepeat (will return a boolean Unique)
if Unique = True Then
Combo1.Additem (strData)
end if
rs.MoveNext
end if
wend

Thanks!!
Nov 2 '06 #2

100+
P: 267
hi
or use 'plain' VB

put

Expand|Select|Wrap|Line Numbers
  1. Public Const CB_FINDSTRING = &H14C
  2. Public Const CB_FINDSTRINGEXACT = &H158
  3.  
  4. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  5.  
in your general declaration

and use

Expand|Select|Wrap|Line Numbers
  1. Ret = SendMessage(cboComboBox.hwnd, CB_FINDSTRINGEXACT, -1, strCompare)
  2. if Ret=-1 then cboComboBox.AddItem strCompare
  3.  
where Ret is your ComboBox listindex
and strCompare is the string your are looking for in your ComboBox.

if you get an error-message regarding the called API
change to lParam As String
Nov 2 '06 #3

Expert 5K+
P: 8,434
... if you get an error-message regarding the called API change to lParam As String
It's been quite a while, but don't you need to define string parms for API calls as ByVal to make VB pass them as null-terminated format?
Nov 2 '06 #4

100+
P: 1,646
Interesting couple of answers.

Sashi gets it handled before it happens while the other is an extremely involved fix after the fact. I would go with DISTINCT every time :)
Nov 2 '06 #5

Expert 5K+
P: 8,434
Interesting couple of answers.
Sashi gets it handled before it happens while the other is an extremely involved fix after the fact. I would go with DISTINCT every time :)
Depends where the info is coming from, of course.
If you can source just the unique values in the first place, that is obviously preferable to filtering them as you populate the control, however the filtering is done.
Nov 2 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.