Hello,
I'm a newbie in vb :D however with all the information and help this forum had provided i reach this far with my code BUT i can't proceed anymore.
I'm trying to make a small software which will help me in my work. I can't get it to work and with so little experience in this field i'm so lost. could you please tell me what i did wrong here.
Imports System.Data.OleDb
Public Class Form2
'+++++++++++++++++++++++++++++++++++++
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:/mydata.mdb")
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cmd As OleDbCommand = New OleDbCommand("SELECT EmployeeID,FirstName FROM Employees ORDER BY FirstName", con)
Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT ProductID, ProductName FROM Products", con)
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim ds2 As New DataSet
Dim da2 As New OleDbDataAdapter
con.Open()
ds = New DataSet()
da = New OleDbDataAdapter()
da.SelectCommand = cmd
da.Fill(ds, "employee")
With cmbdscName
.DataSource = ds.Tables("employee")
.DisplayMember = "FirstName"
.ValueMember = "EmployeeID"
.SelectedIndex = 0
End With
ds2 = New DataSet()
da2 = New OleDbDataAdapter()
da2.SelectCommand = cmd2
da2.Fill(ds2, "Product")
With cmbProduct
.DataSource = ds2.Tables("product")
.DisplayMember = "ProductName"
.ValueMember = "ProductID"
.SelectedIndex = 0
End With
con.Close()
con = Nothing
End Sub
'now what i want to do is to filter the next combo box (cmbsubp) by the value selected in the first combo box (cmbProduct)
Private Sub cmbProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbProduct.SelectedIndexChanged
Dim cmd As OleDbCommand = New OleDbCommand("SELECT subp.SubItem " & _
"FROM subp INNER JOIN Products ON subp.ProductID = Products.ProductID " & _
"WHERE (((Product.ProductID) = '" & cmbProduct.SelectedItem.ToString & "'))", con)
'products table have a productID which is related to the ProductID field in subp table.
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
con.Open() ' I have error here telling me that connection has not been closed
ds = New DataSet()
da = New OleDbDataAdapter()
da.SelectCommand = cmd
da.Fill(ds, "products") 'when i comment out con.open() i have an error here (No value given for one or more required parameters.)
With cmbSubP
.DataSource = ds.Tables("products")
.DisplayMember = "ProductName"
.ValueMember = "ProductID"
.SelectedIndex = 0
End With
con.Close()
End Sub
End Class
well as i have said, i tried to fix it but with the little understanding i have .. i can't :(
The product have a Sub product, so when they select one product from the first combo i want the related sub-product to fill the combo.
if my approach is wrong i would appreciate it if someone can tell me a better approach.