Solved

Adding Records to an Access Database using VB 2005

Posted on 2006-07-02
3
213 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net - How to check if current user is an administrator? 6 47
Help with AsEnumerable(), LINQ 4 40
Variable Event ? 3 27
Close form "before" open 3 40
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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