Removing Duplicates From a ComboBox

johnuk4

Active member
Joined
Feb 18, 2006
Messages
25
Programming Experience
Beginner
Hi, iv got this problem iv been trying to solve for some time now by looking at examples etc of others code. Basically i am inserting data from a access db table, to fill the data for a combo box. I have successfully done this using the following code, however im trying to find away to insert code that will remove the duplicates coming from the db table. This is probably very simple however i am a certified beginnner when it comes to programming so would very much appreciate any help from more experienced programmers

thanks alot

john

Here is the code (sorry about the indentation):

PrivateSub ReportHardwareProblem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
' Retrieve all the Hardware types in the table
' Dim AppPath As String = Mid(Application.ExecutablePath, 1, Len(Application.ExecutablePath) - 14)
Dim ConString AsString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=""ProblemDatabase.mdb"";"
Dim DBCon AsNew OleDb.OleDbConnection(ConString)
DBCon.Open()
Dim DSet AsNew DataSet, SQLStr AsString
Dim cmd As System.Data.OleDb.OleDbCommand
Dim dbAdaptr As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter

With dbAdaptr
.TableMappings.Add("Table", "Companies")
SQLStr = "Select [Hardware_Type] from Hardware"
cmd =
New System.Data.OleDb.OleDbCommand(SQLStr, DBCon)
cmd.CommandType = CommandType.Text
.SelectCommand = cmd
.Fill(DSet)
.Dispose()
EndWith
DSet.AcceptChanges()
tTbl = DSet.Tables.Item(0)
DSet.Dispose()
DBCon.Close()
' fill out array by Company Names for cboRightCombo combobox
cboHardwareType.Text = ""
cboHardwareType.Items.Clear()
cboHardwareType.BeginUpdate()

'Load the Company Names into the ComboBox Control
Dim index AsString
Dim Array() AsString
ForEach tRow In tTbl.Rows
cboHardwareType.Items.Add(tRow("Hardware_Type").ToString)
Next
cboHardwareType.EndUpdate()
EndSub
 
If all you're getting from the database is a single column then you can just use the DISTINCT keyword:
VB.NET:
Expand Collapse Copy
SELECT DISTINCT HardwareType FROM Hardware
BTW, the underscore doesn't really serve a purpose using title case, e.g. HardwareType and HARDWARE_TYPE.
 
Back
Top