Hamza Asim
Hamza Asim

Reputation: 39

'The Microsoft Access database engine cannot open or write to the file' error when opening access database from VB.net

So whenever I try to write some data into one of my databases through VB.net, I'm getting this error which says it's because either the database is open or permission is not granted to me to write or read the database.

I have made sure the permission is grated to 'everyone' for read and write and also there's no database open either. But the error still shows. Having done some research, it says I should move my database on my local server which it already is.

The error occurs in the last line when I run my program:

provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        datafile = "C:\Users\Hamza\Desktop\POS_database"
        connstring = provider & datafile
        myconnection.connectionstring = connstring
        myconnection.Open()

Are there any other ways I could solve this problem?

Upvotes: 0

Views: 2804

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

As suggested in the comments, you are missing the file extension on your data file name. If you're using ACE then, presumably, it is an ACCDB file. You should use a connection string builder and the correct file path:

Dim builder As New OleDbConnectionStringBuilder With
               {
                   .Provider = "Microsoft.ACE.OLEDB.12.0",
                   .DataSource = "C:\Users\Hamza\Desktop\POS_database.accdb"
               }

Using connection As New OleDbConnection(builder.ConnectionString)
    connection.Open()

    'Use connection here.
End Using

Also, if the file is on the desktop of the current user then you shouldn't hard-code the path like that:

.DataSource = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "POS_database.accdb")

Upvotes: 1

Related Questions