473,657 Members | 2,566 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 970

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

Similar topics

4
1993
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 with each row of the first table. I am able to display the first table's data in one of the DataGridView controls - DatgaGridView_A. What I want to do, but don't know how, is to select a row from the DataGridView_A and have the data associated...
1
3835
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 come from to get you proper data. In access I would just filter the query(rewrite its qrydef) and I'd get my answer. My question is, what is the proper way of doing this in ADP and SQL Server 2005. I could filter the data from the view on the...
5
1978
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 names") and then displays all records with company names in a datagridview. Once the information has been binded to the datagridview control, I allow the user to select all records that are not companies, for example, during the search the key word...
0
1186
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 database one to edit an entry in the database and one to close an entry in the database. All three buttons work in the similar way, such that the entry you select in the datagridview is the entry you want to close or edit. When I click on the open or edit...
5
2522
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. This is so simple in VBA but I have searched help, internet and 2 books and have yet to find a working example for VB 2005 Does anyone have a simple sample of opening a table and adding data. I know this an Access group but some here also...
2
2215
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. -------------------------------------- My Problem is this: -------------------------------------- I have 2 forms. The first form contains a DataGridView and a button. The DataGridView is bound to an access database, and loads at form_load. The second form contains several...
1
2251
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). Now, all I'm missing is my pride. The issue: Debugging my form to get the bound datagridview to update the backend database table. After running the app in the debugger, none of the changes I made to the table in the datagridview were being saved...
0
1008
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 created automatically, as well as Calcul_mentalDataSet, ElevesBindingSource, ElevesTableAdapter. The purpose of this form is to enable a user to authenticate by clicking on the line that represents it. And if not in the list, then click a button a...
5
2902
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 best to do it this way, calling the update stored procedure for every update? Or should I be loading this data into a staging table, and if all goes well do the 'Real' Update. Or put this into a data adapter and update from that? The application...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.