0

I have a simple Access database, which contains one table. Here is it Screenshot

For my Button Load event I have this code

    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()
    MsgBox("Opened")
    con.Close()

And on con.Open() line I am getting this exception . And I can not understand what is the problem. Maybe the name "con" was the problem, but I changed it to "c" or "con1" but the same exception occurs. Can't understand the reason. Thanks for any solution

Screenshot 2

Romo Daneghyan
  • 2,099
  • 3
  • 18
  • 23

4 Answers4

0

I don't think Provider is required here, as you already have it in the source, change...

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"

con.ConnectionString = dbProvider & dbSource

to...

dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"

con.ConnectionString = dbSource

Take a look at ConnectionStrings.

A better solution is to add the file to the App_Data folder instead of referencing the file from a local drive. Add the connection string to the config...

<connectionStrings>
    <add name="AccessConnection"
        connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|dbName"
        providerName="System.Data.OleDb" />
</connectionStrings>
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • The same exception :/ – Romo Daneghyan Aug 04 '14 at 11:37
  • Yeah. The same exception in my program. Please advice. Here is the connection string @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪‪G:\Edict18042015.accdb; Persist Security Info=False;"; – Kushan Randima May 06 '15 at 08:25
  • @KushanRandima, instead of placing the file at the route of your *G* drive, consider adding it to the App_Data folder, and adding a connection string in the config - I'll update the answer now. – Christian Phillips May 06 '15 at 08:41
0

put the @ before the connection string. it works for me!

Anmol
  • 8,110
  • 9
  • 38
  • 63
0

I also faced the same problem: but solution is: you must give a correct path of your access database, and don't forget the access file name with its extension (your database and .accdb). I am sure it will be all correct...

Aso
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 04 '22 at 05:54
-1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Documents\k1.accdb"
    conn.ConnectionString = dbprovider
    conn.Open()
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • You should include an explanation of how this would help users. But it's not looking like a very helpful answer at the moment... – LarsTech May 18 '16 at 15:32