A Combo box has two properties
1.One which displays the items in the combo box called the DISPLAY MEMBER
2. Another one which stores the underlying ID for the Item which is being Displayed.
eg your table may have EmployeeId and EmployeeName.
You would like to display the EmployeeName in the ComboBox and when a user selects an employee name you would like to save the EmployeeID.
Function Fillcombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal DisplayMember As String, ByVal ValueMember As String)
Dim dt As New DataTable 'Create a New DataTable
Dim dc As New SqlCommand 'Create a New SQLCommand
Dim da As New SqlDataAdapter ' Create a New SQL Data Adapter
dc.CommandTimeout = 0
dc.CommandText = sql
da.SelectCommand = dc
dc.Connection = cn
da.Fill(dt) ‘ Use the DataAdapter to fill the DataTable with the records
CBO.DataSource = dt
CBO.DisplayMember = DisplayMember ‘ This is the item which will be displayed on the combo box eg Employee Name
CBO.ValueMember = ValueMember ‘ This is the value which is stored eg. employeeID
MsgBox(“Unable to Fill the Combobox:” & CBO.Name & ” with values” & “:” & Err.Description)
How to use the above function
Fillcombo(“Select EmployeeID,EmployeeName from Employees”, Me.EmployeeCombo, “EmployeeName”, “EmployeeID”)