Solved

Data entered in Access table via VB seem to work, but missing next time file is opened.

Posted on 2014-01-05
4
851 Views
Last Modified: 2014-01-13
I have used MS Access 2013 to create a db-file with some data. I have then written some code in VB.NET that opens and access the data. I can create new records, and I can page through them back and forth and all seems fine. But when I run my program again, the data I entered has gone missing. I use
    Private Function storeData(ByVal mySql As String) As Boolean
        Dim ret As Boolean = False
        Dim ac As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myPath)
        Try
            Using ac
                Dim myCommand = New System.Data.OleDb.OleDbCommand(mySql, ac)
                myCommand.Connection.Open()
                myCommand.ExecuteNonQuery()
                myCommand.Connection.Close()
            End Using

            ret = True
        Catch ex As Exception
            MsgBox("There is something wrong." & _
                   vbNewLine & ex.ToString, MsgBoxStyle.Exclamation, "SQL Error")
        End Try
        Return ret
    End Function

Open in new window

The SQL command looks something like this:
Insert into NAD(Name,Address,Postnumber,Telefonnumber,Epostaddress,Webpage) values('Thor','Oslo','0575','999 88 777','epost@epost.no','www.web.com');

Open in new window

and seem to work fine, and when I browse through the table using my program after having entered a new record, I can see the new records. But when I close the program and reopens it, or query the DB from outside, the newly entered data have disappeared. What am I missing? Do I need som kind of file save or commit or something?
0
Comment
Question by:IverErling
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39757907
Check the location of where the Access DB file is. Then go to the programs project folder debug\bin directory and see if there is a copy there, if so open that version of the DB file and see if the records are there.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 400 total points
ID: 39757955
ExecuteNonQuery returns an Integer that indicates the number of rows affected by the command. Check its value.

If it is zero, then the command did not add the data to the table in the database.

If it is more than zero, then the data was inserted. In such a case, as Fernando suggested, you might be looking at 2 copies of the database and might be inserting in the wrong one. If myPath contains a relative path, this can easily arise.

If this is the case and If you see the database file in your project, click on it and look at the Copy to Output Directory in the Properties window. You might need to change it to another value to adjust the way that you work with the database during development.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 39757975
Hi IverErling;

As @JamesBurger stated you might be using a local database file in your project and the reason for my questions in my post. If that is the case this web page will give you the steps you need to follow for the configuration you want.

How to: Manage Local Data Files in Your Project
0
 

Author Comment

by:IverErling
ID: 39763089
Thanks for your advice! I will check this as soon as I get a chance, hopefully within a day or two! So easy to miss such things ;-)

IVer
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

777 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