Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help! I cant save data changes to an MDB file in Visual Basic 2005 Express

Hi all.

I've created an application that will read data from an MDB table (linked to an Excel spreadsheet), but I during debugging I cannot actually save any changes made to the table. I've gone through all the walkthroughs but I just end up with an error message.

Is it actually possible to write to an MDB file? If not, how do I create an MDF file (which I know is writable) from an existing Excel spreadsheet? Although I have created a version of this spreadsheet on my local server, VB2005E won't let me connect to my local server when choosing a data source.

Clearly I'm a complete newbie at this, so all advice is very gratefully appreciated.
[695 byte] By [AngusHuey] at [2007-11-11 10:14:12]
# 1 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Yes, you should be able to write to an MDB file. What error do you get? Please post the code that's causing the error.
Phil Weber at 2007-11-11 20:48:22 >
# 2 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
(Apologies, just realised I posted this on the wrong board)

Thanks Phil. This is the error message when I try and save any changed data:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

And this is the code for the save button:

Private Sub GaiaDataBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GaiaDataBindingNavigatorSaveItem.Click
Me.Validate()
Me.GaiaDataBindingSource.EndEdit()
Me.GaiaDataTableAdapter.Update(Me.GaiaDataSet.GaiaData)

End Sub
AngusHuey at 2007-11-11 20:49:22 >
# 3 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
What is the value of the GaiaDataTableAdapter's UpdateCommand property?
Phil Weber at 2007-11-11 20:50:26 >
# 4 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Sorry Phil - as if to really emphasise my newbieness - how do I go about finding this?
AngusHuey at 2007-11-11 20:51:31 >
# 5 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Phil
I use this code to try and update a table in a MDF and get the same error he is getting.
Private Sub EmployeesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeesBindingNavigatorSaveItem.Click
Me.Validate()
Me.EmployeesBindingSource.EndEdit()
Me.EmployeesTableAdapter.Update(Me.NORTHWNDDataSet.Employees)

End Sub
BennettDan at 2007-11-11 20:52:30 >
# 6 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Angus: No problem, I had some difficulty finding it myself. ;-) To be honest, I'm not very experienced with Visual Studio 2005 and all the code it generates automatically. The simplest way to find the UpdateCommand is to do a Quick Find (Edit -> Quick Find, or Ctrl+F) and search the Current Project for "UpdateCommand.CommandText". You should find it in the file GaiaDataSet.Designer.vb.

I created a Data Connection to an Access MDB and dragged a table from the Data Sources window onto a form. I am able to add or edit a row in the grid and save the row to the database without error. The problem may be that you don't have a key field which uniquely identifies each row; that's required in order for the UpdateCommand to know which record to update.
Phil Weber at 2007-11-11 20:53:30 >
# 7 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Phil, thanks again for this. Here's the relevant code in GaiaDataSet.Designer.vb:

<System.Diagnostics.DebuggerNonUserCodeAttribute()> _
Friend Property Connection() As System.Data.OleDb.OleDbConnection
Get
If (Me._connection Is Nothing) Then
Me.InitConnection
End If
Return Me._connection
End Get
Set
Me._connection = value
If (Not (Me.Adapter.InsertCommand) Is Nothing) Then
Me.Adapter.InsertCommand.Connection = value
End If
If (Not (Me.Adapter.DeleteCommand) Is Nothing) Then
Me.Adapter.DeleteCommand.Connection = value
End If
If (Not (Me.Adapter.UpdateCommand) Is Nothing) Then
Me.Adapter.UpdateCommand.Connection = value
End If
Dim i As Integer = 0
Do While (i < Me.CommandCollection.Length)
If (Not (Me.CommandCollection(i)) Is Nothing) Then
CType(Me.CommandCollection(i),System.Data.OleDb.OleDbCommand).Connection = value
End If
i = (i + 1)
Loop
End Set
End Property

Incidentally, when I do set a field as a primary key I also get the following error message:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

The AllowDBNull on the field ('Publications') to which I have set the primary key is set to False, if that helps.
AngusHuey at 2007-11-11 20:54:24 >
# 8 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Thanks, but I still don't know the value of UpdateCommand.CommandText. ;-)

The second error message would seem to indicate that you have one or more duplicate values in your primary key field. Can you post a small sample (a dozen or so records) of the data you're working with?
Phil Weber at 2007-11-11 20:55:28 >
# 9 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
The text "UpdateCommand.CommandText" is not found anywhere within the project... should it be there?

I've sorted the Primary Key problem by changing the Enforce Constraints value on that field to False.
AngusHuey at 2007-11-11 20:56:26 >
# 10 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Yes, that's the first error message you were getting:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

If possible, please post a sample of your data.
Phil Weber at 2007-11-11 20:57:27 >
# 11 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Here is a screenshot ( http://img20.imageshack.us/img20/764/angushueydatajv9.jpg) of the first 10 rows (out of about 1100) of my data, along with the column headers:
AngusHuey at 2007-11-11 20:58:30 >
# 12 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Phil How do we go about adding the "UpdateCommand.CommandText"?
BennettDan at 2007-11-11 20:59:39 >
# 13 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
The primary key field may not contain null values. If you're using the "Code" field as your primary key, assign a unique code to the first record, "Albany & Great Southern Weekly" (and to any other records with blank codes).

Now, in a new Visual Basic Windows Forms project, I select Data -> Add New Data Source...

On the first page of the Data Source Configuration Wizard, I select "Database."

On the next page, I select my connection to the GaiaData.mdb file.

On the third page, I choose not to save the connection string in the config file, but that's irrelevant.

On the Database Objects page, I select the GaiaData table in its entirety and name the DataSet, "GaiaDataSet." Finally, I click Finish.

Now, I select Data -> Show Data Sources and drag the GaiaData table onto a blank form. Visual Studio creates a DataGridView, BindingNavigator, TableAdapter and BindingSource and wires everything up. I press F5 to run the app.

I add a new row to the grid and hold my breath as I click the Save button on the BindingNavigator. The Save seems to succeed; I receive no error messages. I open the database in Access to make sure: yes, my new row has been saved to the database.
Phil Weber at 2007-11-11 21:00:35 >
# 14 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Phil, yeah that's exactly how I've been doing it for the last week or so, without any success - I still come up with the "Update requires a valid UpdateCommand when passed DataRow collection with modified rows" error message whilst debugging.

There is no "UpdateCommand.CommandText" variable anywhere in the project, which makes me think it needs to be added manually as a TableAdapter - either that or there's something wrong with the original database?

I'm getting a little frustrated, and it doesn't help that my boss is beathing down my neck to get this finished! :(
AngusHuey at 2007-11-11 21:01:36 >
# 15 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Visual Studio cannot autogenerate Insert- and UpdateCommands unless you have a unique key for each row; I think that's been your problem all along. Make sure you designate a field as your primary key, that that field contains a unique value for each row, and that you reset Enforce Constraints to True for that field.

It should just work!
Phil Weber at 2007-11-11 21:02:34 >
# 16 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Phil, I've just realised there is a single duplicated cell in the Publication field (which I set as primary, assuming there would be no duplicates) on my original database, so I guess I'll recreate the MDB and start again!
AngusHuey at 2007-11-11 21:03:33 >
# 17 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
I've recreated the database with no duplicate data in the Primary Key field (and I had to turn off Enforce Constraints or it wouldn't run), but it still created the TableAdapter without an Update command.

Now I've finally found a way to manually insert a new UpdateCommand*, but the query (below) is coming up with a syntax error. I'm assuming there needs to be a value after the equal sign, but obviously I want the value to be whatever the user has input:

UPDATE GaiaData
SET State =, Type =, Code =, Suburb =, Circulation =, Contact =, [Day of Publication] =, [Subscription Ends] =, [Last Date Processed] =, [Most Recent Edition] =

So in answer to your much earlier question (!) this is my current value for the UpdateCommand.CommandText.

(*Apparently this is supposed to be done automatically, but for some reason didn't build on my app. Right click on your dataset in Solution Explorer, choose 'View Designer', rightclick on the XXXTableAdapter heading, and choose properties. The InsertCommand and SelectCommand had values, but my UpdateCommand was set to 'none'. So I added one.)
AngusHuey at 2007-11-11 21:04:42 >
# 18 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
What then should the values be set to, to update what the user has inputted?
AngusHuey at 2007-11-11 21:05:40 >
# 19 Re: Help! I cant save data changes to an MDB file in Visual Basic 2005 Express
Well, after all that struggling I finally figured out why VBExpress wasn't automatically creating Update and Delete Commands - I had manually set the Primary Key in VBExpress instead of allowing Access to set it on my original database. So I went back and imported the original database from Excel into Access, allowed Access to insert an ID column (which I later hid in VBExpress) and set it as Primary.

Creating the app again from scratch in VBExpress then correctly generating the correct commands in the TableAdapter.

Thanks to everyone for their help... I'm sure I'll be back for more assistance!
AngusHuey at 2007-11-11 21:06:38 >