I have several comboboxes on various tabs which I want to load from the database to give a simple list of student names and group names.
The problem is, if I have 2 comboboxes for the student names, the data is repeated twice in both comboboxes. And if I have 2 comboboxes for the Groups, the data is repeated 5 times in all the comboboxes.
The code I have is below.
Why is it repeating the data the same number of times as there are comboboxes and how do I solve it?
The problem is, if I have 2 comboboxes for the student names, the data is repeated twice in both comboboxes. And if I have 2 comboboxes for the Groups, the data is repeated 5 times in all the comboboxes.
The code I have is below.
vb.net Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Visible = True Dim SQL As String = "SELECT stID, stName FROM tblStudent ORDER BY stName" Dim Val As String = "stID" Dim disp As String = "stName" FillCombo(SQL, cboName, Val, disp, "Student") FillCombo(SQL, cboStudentName, Val, disp, "Student") SQL = "SELECT grID, grName FROM tblGroup ORDER BY grName" Val = "grID" disp = "grName" FillCombo(SQL, cboStudentGroup, Val, disp, "Group") FillCombo(SQL, cboRegistersGroup, Val, disp, "Group") FillCombo(SQL, cboReportsGroup, Val, disp, "Group") FillCombo(SQL, cboAttendanceGroup, Val, disp, "Group") FillCombo(SQL, cboMaintGroupName, Val, disp, "Group") End Sub Private Sub FillCombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal val As String, ByVal disp As String, ByVal tblName As String) dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0" dbSource = "Data Source=" & Application.StartupPath & "\Student.mdb" Conn.ConnectionString = dbProvider & ";" & dbSource Conn.Open() da = New OleDb.OleDbDataAdapter(SQL, Conn) da.Fill(ds, tblName) Conn.Close() CBO.DataSource = ds.Tables(tblName) CBO.ValueMember = val CBO.DisplayMember = disp End Sub