Solved

Adding Records to an Access Database using VB 2005

Posted on 2006-07-02
3
194 Views
Last Modified: 2010-04-23
I am trying to add records to an existing Access datatable. The dataset,  bindingsource, and tableadapter are defined.  The following programs runs ok. I can see the new records in a datagrid view, but the source access database is unchanged after I close the program.  

Sub AddRecords()
        Dim dt As DataTable
        Dim dr As DataRow
        dt = DbTestDataSet.EventIndex      'the access dataset & datatable

        EventIndexTableAdapter.Fill(dt)

        Dim dr As DataRow
        For i = 1 To 5
            dr = dt.NewRow()
            dr!event = i * 2
            dr!timesim = i - 4
            dr!date = Now
            dr!duration = 89
            dt.Rows.Add(dr)
        Next
        dt.AcceptChanges()
        EventIndexTableAdapter.Update(dt)
       
0
Comment
Question by:BW999
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 17028426
Hi BW999,
>         dt.AcceptChanges()
Try remarking this line and see how it goes

Dabas
0
 

Author Comment

by:BW999
ID: 17033724
Dabas,

The problem was that the dataset was referenced to the wrong file.  Apparently VB makes a copy of the database and stores it in a working directory somewhere. So it was making changes to copied database instead of mine. I thought it was supposed to copy that back to my original file when the changes were accepted.  But apparently not for some reason.

I changed the file reference in the Project/ Properties/Settings window to the actual source file instead of the copy.  After that, it works, whether or not the 'dt.acceptchanges' line is included.

It works, but not as advertised.  Any idea why the file refencet would be messed up?

BW
0
 
LVL 27

Accepted Solution

by:
Dabas earned 250 total points
ID: 17033850
BW999,
I am glad you have solved your problem
As far as I can tell, the AcceptChanges name is misleading.
What most people understand is something like "This dataset (or datatable) has changed. Please accept them before updating"
Internally vb KNOWS that you have made changes, and actually remembers which rows have changed by setting a flag on each of the changed rows. When you say AcceptChanges, vb understands you are asking it to unmark all of those flags. Theferore when you call update, nothing gets changed in your original data

You are right. The changes to the data are on a local copy (I presume in memory, not in a working directory). When you call UPDATE, the data in the original source gets updated. This is one of the powerful features of ADO.NET, and it takes into account that your source could be half way around the world. Hence instead of keeping a connection to your source open (which can be very slow if your source is far away), it only communicates when you issue the update statement, which can happen after many changes have been done, like in your case (It would be wrong to do the .Update inside the loop, for example)

Dabas
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now