473,503 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Updating Access table from datagridview

15 New Member
I have an application that allows the user to select a table name from a combobox. Once the table is selected it fills in a DataGridView with the info from that table. I need to let them make any changes then update the info back into access. Below is my code to get the data into the DGV:

Expand|Select|Wrap|Line Numbers
  1.  Private Function LoadTableData() As Boolean
  2.         Try
  3.  
  4.             Dim cmd As New OleDb.OleDbCommand("SELECT * FROM " & cmbTables.Text, c)
  5.             '  Dim ds As New DataSet
  6.             Dim dt As New DataTable
  7.             Dim da As New OleDb.OleDbDataAdapter(cmd)
  8.             dt.TableName = cmbTables.Text
  9.             ds.Tables.Add(dt)
  10.  
  11.             c.Open()
  12.             da.Fill(ds, cmbTables.Text)
  13.             Dim cb As New OleDb.OleDbCommandBuilder(da)
  14.             da.InsertCommand = cb.GetInsertCommand()
  15.             da.UpdateCommand = cb.GetUpdateCommand()
  16.             da.DeleteCommand = cb.GetDeleteCommand()
  17.             c.Close()
  18.  
  19.             Me.MachinesBindingSource.DataSource = ds
  20.             Me.MachinesBindingSource.DataMember = cmbTables.Text
  21.             Me.DataGridView1.AutoGenerateColumns = True
  22.  
  23.  
  24.             '  Me.DataGridView1.ReadOnly = True
  25.  
  26.             Return True
  27.         Catch ex As Exception
  28.             MsgBox(Err.Description)
  29.             Return False
  30.         End Try
  31.     End Function
When the user is finished changing data, they will click the save button and the new data in the table sent to the database. How do I do that? Is there a way to update the entire table?

This is the code I have in my "save" button click:

Expand|Select|Wrap|Line Numbers
  1. If txtReason.Text <> "" Then
  2.             'write change to database
  3.             ' MsgBox(strCurrentUser & " changed column " & ColumnThatWasChanged & " for record " & DataGridView1.Rows(CurrentRow).Cells(0).Value.ToString() & " in table " & cmbTables.Text & " from " & PreviousValue & " to " & NewValue)
  4.  
  5.  
  6.             Dim da As New OleDb.OleDbDataAdapter
  7.             Dim cmd As New Data.OleDb.OleDbCommand
  8.  
  9.             If ds.HasChanges() Then
  10.                 c.Open()
  11.                 Me.Validate()
  12.                 Me.MachinesBindingSource.EndEdit()
  13.                 da.Update(ds, cmbTables.Text)
  14.                 'da.Update(ds, cmbTables.Text)
  15.                 c.Close()
  16.             End If
  17.         Else
  18.             MsgBox("You must enter a reason for the change.")
  19.         End If
but it generates :"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
Thanks,
Jason
Nov 16 '07 #1
0 959

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1975
by: Steve Teeples | last post by:
I have a form with two DataGridView controls. The form has linked to it an Access database with just two tables. The second table contains unique "child" data having a one-to-one relationship...
1
3823
by: ken | last post by:
Hi, Lets say we have a form that displays time card entries based on the calendar control date on the form. So the user clicks on a date and the form filters the table where the time card entries...
5
1963
by: JimmyKoolPantz | last post by:
Situation: I am writing a program that opens up a data file (file ext .dbf), imports the information into a dataset. The program also, searches through the dataset for key words ("company...
0
1174
by: twigboy | last post by:
Hi everyone, I have a strongly typed dataset, with my main form having a datagridview that is binded to an access database. The main form has three buttons one to create a new entry in the...
5
2510
by: Karl | last post by:
I normally only use A2000 but I need to do a little project in VB2005 But I can't figure out how to open the backend Access table in code and add data using code. I don't need to bind to a form....
2
2213
by: snowdog17 | last post by:
Hello, I am a student and I need help with my VB task. I am currently using VB 2005 Express and I am fairly new to it, although I have programed in Delphi before....
1
2241
srj115
by: srj115 | last post by:
This is just something that had me tearing out my nose hairs in frustration... hypothetically. I must have spent 7 or 8 hours trying to get this to work before I smelled the coffee (so to speak). ...
0
1005
by: Pascal | last post by:
Hello I began with vbnet and databinding. I created a database containing a table access Students. In a form frmListEleves I slipped and deposited the table in question and a datagridview was...
5
2894
by: Bill Schanks | last post by:
I have a winform app (VB 2005) that allows users to export data to excel, make updates to the excel file and import the data from that Excel file and update the database. My question is: Is it...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.