Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If blnLoadListbox1 = True Then ' Here I check if listbox1 is filled
Dim strSQLGroup As String
'In the following code I check if an item is clicked in listbox1, if so then selected index of listbox1 will be different from -1 and then has the table tblGroup that fills listbox2 and is a table of the dataset to be cleared so it's ready to be filled by a new dataadapter.
If Me.ListBox1.SelectedIndex > -1 Then
Me.dataset.Tables("tblGroup").Clear()
End If
'Here I create the sqlstring for the dataadapter that has to fill the datatable tblGroup that is a part of the dataset
Try
strSQLGroup = "SELECT GroupDescription FROM tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue.ToString
Dim DAGroup As OleDbDataAdapter = New OleDbDataAdapter(strSQLGroup, connection)
DAGroup.Fill(dataset, "tblGroup")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'Here I fill the listbox with the contents of dataset.tblGroup
Me.ListBox2.DataSource = dataset.Tables("tblGroup")
Me.ListBox2.DisplayMember = "GroupDescription"
End If
End Sub