Solved

Adding Records to an Access Database using VB 2005

Posted on 2006-07-02
3
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

631 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