I am currently using a DataGridView to display data from multiple tables within a Microsoft Access database. However as i mentioned in an earlier thread i get the error saying data from multiple tables cannot be updated as i am using a command builder. Could anybody use this code to show me what changes i need to make to fix the problem? If you can give me a solution you would literally be a lifesaver :')
Imports System.Data.OleDb
Public Class frmOrderhistory
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbDataAdapter As OleDbDataAdapter
Dim connection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\MainDatabase.accdb;")
Dim dtSearchCustomer As DataTable
Dim myDataSet As DataSet = New DataSet("SearchOrder") 'All declared global variables so any sub on the form can access the
Private Sub frmOrderhistory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As String
sql = "SELECT CustomerDetails.ContactName, ProductDetails.ProductName, OrderCreations.Quantity, OrderHistory.DateOfPurchase FROM ProductDetails INNER JOIN ((CustomerDetails INNER JOIN OrderHistory ON CustomerDetails.CustomerID = OrderHistory.CustomerID) INNER JOIN OrderCreations ON OrderHistory.OrderID = OrderCreations.OrderID) ON ProductDetails.ProductID = OrderCreations.ProductID"
Try
connection.Open()
dbDataAdapter = New OleDbDataAdapter(sql, connection) ' send sql to the database
dbDataAdapter.Fill(myDataSet, "SearchOrder") ' fill the dataset with the result from the query
DatagrdOrderHistory.DataSource = myDataSet.Tables("SearchOrder").DefaultView ' link the datagridview to the dataset
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
PrintOrderHistory.Print() 'Prints the form, so the order history being searched can be printed
End Sub
Private Sub lblReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblReturn.Click
Me.Hide()
frmMain.Show()
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim Keywords As String = txtCustName.Text
Dim sqlsearch As String
sqlsearch = "SELECT CustomerDetails.ContactName, ProductDetails.ProductName, OrderCreations.Quantity, OrderHistory.DateOfPurchase FROM ProductDetails INNER JOIN ((CustomerDetails INNER JOIN OrderHistory ON CustomerDetails.CustomerID = OrderHistory.CustomerID) INNER JOIN OrderCreations ON OrderHistory.OrderID = OrderCreations.OrderID) ON ProductDetails.ProductID = OrderCreations.ProductID WHERE (((CustomerDetails.ContactName)='" & txtCustName.Text & "'))"
dbDataAdapter = New OleDbDataAdapter(sqlsearch, connection)
myDataSet.Clear()
dbDataAdapter.Fill(myDataSet, "SearchOrder")
DatagrdOrderHistory.DataSource = myDataSet.Tables("SearchOrder").DefaultView
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
DatagrdOrderHistory.AllowUserToDeleteRows = True 'Allows user to delete rows
DatagrdOrderHistory.ReadOnly = False 'Allows user to edit cells within the data grid
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(dbDataAdapter)
dbDataAdapter.Update(myDataSet, "SearchOrder")
DatagrdOrderHistory.AllowUserToDeleteRows = False 'Disables user being able to delete rows
DatagrdOrderHistory.ReadOnly = True 'Disables user being able to edit cells within the data grid
End Sub
End Class
Quote:
Imports System.Data.OleDb
Public Class frmOrderhistory
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbDataAdapter As OleDbDataAdapter
Dim connection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\MainDatabase.accdb;")
Dim dtSearchCustomer As DataTable
Dim myDataSet As DataSet = New DataSet("SearchOrder") 'All declared global variables so any sub on the form can access the
Private Sub frmOrderhistory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As String
sql = "SELECT CustomerDetails.ContactName, ProductDetails.ProductName, OrderCreations.Quantity, OrderHistory.DateOfPurchase FROM ProductDetails INNER JOIN ((CustomerDetails INNER JOIN OrderHistory ON CustomerDetails.CustomerID = OrderHistory.CustomerID) INNER JOIN OrderCreations ON OrderHistory.OrderID = OrderCreations.OrderID) ON ProductDetails.ProductID = OrderCreations.ProductID"
Try
connection.Open()
dbDataAdapter = New OleDbDataAdapter(sql, connection) ' send sql to the database
dbDataAdapter.Fill(myDataSet, "SearchOrder") ' fill the dataset with the result from the query
DatagrdOrderHistory.DataSource = myDataSet.Tables("SearchOrder").DefaultView ' link the datagridview to the dataset
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
PrintOrderHistory.Print() 'Prints the form, so the order history being searched can be printed
End Sub
Private Sub lblReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblReturn.Click
Me.Hide()
frmMain.Show()
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim Keywords As String = txtCustName.Text
Dim sqlsearch As String
sqlsearch = "SELECT CustomerDetails.ContactName, ProductDetails.ProductName, OrderCreations.Quantity, OrderHistory.DateOfPurchase FROM ProductDetails INNER JOIN ((CustomerDetails INNER JOIN OrderHistory ON CustomerDetails.CustomerID = OrderHistory.CustomerID) INNER JOIN OrderCreations ON OrderHistory.OrderID = OrderCreations.OrderID) ON ProductDetails.ProductID = OrderCreations.ProductID WHERE (((CustomerDetails.ContactName)='" & txtCustName.Text & "'))"
dbDataAdapter = New OleDbDataAdapter(sqlsearch, connection)
myDataSet.Clear()
dbDataAdapter.Fill(myDataSet, "SearchOrder")
DatagrdOrderHistory.DataSource = myDataSet.Tables("SearchOrder").DefaultView
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
DatagrdOrderHistory.AllowUserToDeleteRows = True 'Allows user to delete rows
DatagrdOrderHistory.ReadOnly = False 'Allows user to edit cells within the data grid
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(dbDataAdapter)
dbDataAdapter.Update(myDataSet, "SearchOrder")
DatagrdOrderHistory.AllowUserToDeleteRows = False 'Disables user being able to delete rows
DatagrdOrderHistory.ReadOnly = True 'Disables user being able to edit cells within the data grid
End Sub
End Class