OleDb Excel Connection Error "System.Data.OleDb.OleDbException" - vb.net

I've created a small vb.net application that connects to a an excel sheet on a shared folder.
It works great if i open the excel before using the the application , the issue is when the excel isn't open in the background i get a "System.Data.OleDb.OleDbException" error when trying to open a connection.
I've read a bit about this error and i understand it has something to do with access rights to a local temp library.
So my questions are:
1) Is there a solution?
2) Is this the best connection strategy for my situation where the excel files sit on a shared drive?
Connection code:
skuPath = "C:\path.xlsm"
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + skuPath + ";Extended Properties=Excel 8.0;")
q1 = "select * from [" + year + "$B4:V128]"
da = New System.Data.OleDb.OleDbDataAdapter(q1, cn)
cn.Open()
da.Fill(ds, "Table1")
cn.Close()
dt = ds.Tables(0)
I receive the error on cn.Open().

Got exactly the same problem few weeks ago.
Error is caused by incorrect version of oledb provider, just as Steve suggested.
Try this:
skuPath = "C:\path.xlsm"
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + skuPath + ";Extended Properties=""Excel 12.0;HDR=Yes""")
q1 = "select * from [" + year + "$B4:V128]"
da = New System.Data.OleDb.OleDbDataAdapter(q1, cn)
cn.Open()
da.Fill(ds, "Table1")
cn.Close()
dt = ds.Tables(0)

Related

Windows 10 : Cannot run "Microsoft.ACE.OLEDB.12" Error

I am using Windows 10 Home (x64), when I run the application (x86 -> read .xlsx).
The application just show me
"The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine"
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xcFileInfo.ToString + ";Extended Properties=Excel 12.0;")
con.Open()
table = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim sheetname As String = table.Rows(0)("table_name").ToString
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter("select * from [" + sheetname + "]", con)
myDA.Fill(myTable)
End Using

Import Excel data to Access database

I want to import data from Excel to Access like this, it's working when my Access database has no password, but not working when it has a password.
I copy this code from internet.
Where do I have to put the password of Access?
Dim Access As String = "C:\FWS\Database1.accdb"
Dim Excel As String = "C:\New folder\2h.xlsx"
' Dim connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Excel & ";Extended Properties=Excel 8.0;"
Dim connect As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excel + ";Extended Properties=""Excel 12.0 Xml;HRD=NO"""
Using conn As New OleDbConnection(connect)
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & "].[Password=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Change the cmd.commandText to be like this:
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";
PWD=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"
From a quick search:
https://www.connectionstrings.com/access/
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;"
http://www.sqlstrings.com/ms-access-connection-strings.htm
"Driver= {MicrosoftAccessDriver(*.mdb)}; DBQ=C:\App1\Your_Database_Name.mdb;Uid=Your_Username; Pwd=Your_Password;"
Or
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\Your_Database_Name.mdb; User Id=admin; Password="
NOTE: These are untested.
please use your db password like my code.my password is 123.
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";PWD=**123**].[Sheet1] SELECT * FROM [Sheet1$]"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
cmd.ExecuteNonQuery()
MessageBox.Show("succussfully updated")
Form4.Show()
Me.Hide()

VB.net get first excel sheet name with OleDB Connection

I'm using following code to connect with a Excel file with OleDB:
Connection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0;")
Command = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", Connection)
DataSet = New System.Data.DataSet
Command.Fill(DataSet)
Connection.Close()
I would like to get the name of the first excel sheet and put it in a string variable so I can use it in the SQL command. How can I do this?

could not find installable isam [vb.net]

I have the following problem, by using the connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=Excel 12.0 Xml;"
Then, I can execute the open-task. Nevertheless, if I want to use the following connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=Excel 12.0 Xml;HDR=NO"
I get a mistake, saying me that the installable isam could not be found.
What is wrong with the second connection string, as I need this one because in my worksheet no headers are used.
Thanks in advance
if you want to query excel you must have a '$' sign after the sheet name in order to define the sheet as a table.
anyway that is the code i wrote for that purpose:
Try
Dim Myconnetion As New OleDbConnection
Dim DataSet As System.Data.DataSet
Dim MyCOmmand As System.Data.OleDb.OleDbDataAdapter
Dim Path As String = fullpath
Myconnetion = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0;")
MyCOmmand = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & "[" & sheetname & "$]")
DataSet = New System.Data.DataSet
MyCOmmand.Fill(DataSet)
dgv.DataSource = DataSet.Tables(0)
Myconnetion.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

need help connecting ms access to vb.net

I need help my program don't the use connection string that i want instead it find the access file on this folder and display error
Could not find file 'C:\Users\user\Documents\Visual Studio 2008\Projects\Patientt\Patientt\bin\Debug\db_hospital.accdb'.
here is my code
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\db_hospital.accdb"
con.Open()
sql = "SELECT * FROM tblPatients"
ds.Clear()
da = New OleDbDataAdapter(sql, con)
da.Fill(ds)
Me.DataGridView1.DataSource = ds.Tables(0)
con.Close()
Put your access file in the debug folder and change the connection string like this
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\db_hospital.accdb"