Unrecognised database format when build connection to excel file - excel

I'm trying to build an add-in which will build a connection to an optional database (excel or access file).
It's fine with access but when try with an Excel file the below error happens:
"Unrecognized database format"
Sub export_data()
Dim conn As ADODB.Connection
Dim flg As FileDialog
Dim dtpth As String
Set flg = Application.FileDialog(msoFileDialogFilePicker)
With flg
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add Description:="Database file type", Extensions:="*.xls,*.xlsx,*.xlsm,*.accdb"
.Filters.Add Description:="All types", Extensions:="*.*"
.Title = "Chon file database"
If .Show = -1 Then
dtpth = .SelectedItems(1)
Sheet2.Range("A1").Value = dtpth
End If
End With
Set conn = New ADODB.Connection
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
If Sheet2.Range("A2").Value = "*ls*" Then
.ConnectionString = "Data Source=" & dtpth & "Extended properties='Excel 16.0 xml;HDR=Yes;IMEX=1';"
.Open
Else
.ConnectionString = "Data Source=" & dtpth & ";"
.Open
End If
End With
End Sub
Cause i'm new in this section, i can't debug the code by myself
Could you please point out what make the code be wrong? I try research somewhere but most of them in access not excel
Thank you very much

You are missing a semicolon in your connectionstring, and probably the quotes if your filename contains spaces.
The line
.ConnectionString = "Data Source=" & dtpth & "Extended properties='Excel 16.0 xml;HDR=Yes;IMEX=1';"
should look like
.ConnectionString = "Data Source='" & dtpth & "';Extended properties='Excel 16.0 xml;HDR=Yes;IMEX=1';"
Here's a good reference for the possible connectionstring formats.

Related

Run time error '3706': Provider cannot be found - Excel VBA "Provider=Microsoft.Jet.OLEDB.4.0"

I saw that this error isn't new, but I can't find the solution.
I have one xls file that use one sheet like as db and with ADODB i get the recordsets that I need.
The code is Very simple and work right for each pc(5) that I tested, with WIN7, WIN10, 32 or 64 bit.
But I've on PC, it's customer Pc, that get me this error: Run time error '3706': Provider cannot be found,
I has checked the WIN version, the office version, they are the same like other PC, WIN10 64 Bit, MS Office 32Bit
There are more control that I've to do to resolve this problem?!?!
thanks for any suggestions
fabrizio
My xls file have 2 sheet, 1th named "dati" with two columns (Anno, Pezzi), 2th named "test" empty, this is the code:
Sub testConn()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set rs = New ADODB.Recordset
#If Win64 Then
cn.Open "Provider=Microsoft.Jet.OLEDB.12.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 8.0;HDR=Yes;"";"
#Else
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 8.0;HDR=Yes;"";"
#End If
strsql = "SELECT anno, Sum(Pezzi)as Tpz from [dati$] group by anno"
rs.Open strsql, cn, adOpenStatic, adLockReadOnly, adCmdUnspecified
rs.MoveFirst
With Worksheets("test")
.Cells.ClearContents
.Range("A1") = "Anno"
.Range("B1") = "T.Pz"
.Range("A2").CopyFromRecordset rs
.Activate
.Select
End With
End Sub
these references was added into file:
Microsoft ActiveX Data Objects 6.1 Library
Microsoft ActiveX Data Recordset 2.8 Library
This works, there are some small details you use not suitable. Version 12, driver is ace not jet,and Extended Properties also is Excel 12.0
And no need to add library.
Sub testConn()
Dim cn As Object
Dim rs As Object
Dim strsql As String
Dim connString
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
If Application.Version < 12 Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 8.0;HDR=Yes;"";"
Else
connString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 12.0;HDR=Yes;"";"
End If
cn.Open connString
strsql = "SELECT anno, Sum(Pezzi) as Tpz from [dati$] group by anno"
Set rs = cn.Execute(strsql)
With Worksheets("test")
.Cells.ClearContents
.Range("A1") = "Anno"
.Range("B1") = "T.Pz"
.Range("A2").CopyFromRecordset rs
.Activate
.Select
End With
End Sub

Getting errors while trying to open a Excel file with ADO

I'm trying to get some values from an Excel file (.xls) using ADO with SQL queries. However I'm getting error while trying to open the connection. This is what I have currently:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = IIf(strPath <> "", strPath, Range("G13").Value)
If strFile = "" Then Exit Sub
strCon = "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";'Extended Properties='Excel 8.0;HDR=No;IMEX=1';"
strCon2 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & ";'Extended Properties='Excel 12.0 Xml;HDR=YES';"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon2
I have tested using both my connection strings (strCon, strCon2) with no success. I either get an error with no text only an "x" symbol. Or a "System Error &H80004005 (-2147467259)"
Edit:
Changed it so I open recordset instead, however it still gives me the same error.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = IIf(strPath <> "", strPath, Range("G13").Value)
If strFile = "" Then Exit Sub
strCon = "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & _
strFile & "';'Extended Properties='Excel 8.0;HDR=No;IMEX=1';"
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strFile & ";" & _
"Extended Properties=Excel 12.0;"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
'cn.Open strCon
strSQL = "SELECT * FROM [Sheet11$C5]"
rs.Open strSQL, strConn
Debug.Print rs.GetString
It's too late to answer this question, but for future readers.
I had a similar problem.
I tried to connect Excel with MySQL database, but when the recordset open, it cracks and shows me the &H80004005 error.
Finally, the problem was my office is 32 bits but my ODBC was 64 bits and this did not like it.
I changed the ODBC connection to 32 bits and the VBA function worked.

Use SQL to Query another excel file

I'm try an alternative to solve this issue using VBA to get the data.
The following code works fine when "Tbltest" is a range but not working when the name represent a table. How can I make it work with Table not range.
Function ConnectToExelPRJReview()
Dim Xlpath As String
On Error GoTo ErrHandlerconnection:
Dim Xlcn As ADODB.Connection
Dim Xlrs As ADODB.Recordset
Xlpath = "\\xxx.xxx.xxxxxx.net\project_review\wip\" & "filetotest.xlsm"
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Xlpath _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set Xlcn = CreateObject("ADODB.Connection")
Set Xlrs = CreateObject("ADODB.Recordset")
Xlcn.Open strCon
strSQL = "SELECT * FROM [Tbltest]"
Xlrs.Open strSQL, Xlcn
'display the first data
MsgBox Xlrs.Fields(0).Value
Xlcn.Close
Exit Function
ErrHandlerconnection:
MsgBox Err.Number & " " & Err.Description, vbCritical, "EXCEL CONNECTION ERROR"
End Function

The Microsoft Office Access database engine could not find the object 'sheet1$'

So, I was creating a simple application in vb6. It is to display the Excel sheet1 contents into Data grid view.
If CommonDialog1.FileName = "" Then
Label2.Caption = "No file selected."
Else
file_name = CommonDialog1.FileName
Label2.Caption = file_name
End If
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= file_name;Extended Properties=Excel 12.0;"
If cn.State = adStateOpen Then
MsgBox "Connected to Excel File"
End If
strQuery = "SELECT * FROM `Sheet1$`" <<Error Highlighted>>
Set rs = cn.Execute(strQuery)
rs.Close
cn.Close
But I receive this error:
Microsoft Office Access database engine could not find the object
'sheet1$'. Make sure the object exists and that you spell its name and
the path name correctly.
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= file_name;Extended Properties=Excel 12.0;"
Here the filename is literally "file_name" - you need to concatenate the variable into the connection string:
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
file_name & ";Extended Properties=Excel 12.0;"

Is there a way to use OLE DB Provider for Jet on an unsaved Excel workbook?

I am working with the Microsoft OLE DB Provider for Jet to execute queries on spreadsheets in Excel using VBA. Is there a way to execute the following code on an unsaved workbook?
For example, ActiveWorkbook.FullName returns "Book1" if the workbook has never been saved. In that case the Data Source will assume the path is the active directory, and error out because the file was never saved.
Is there any way to use the Excel temporary file as the Data Source for Jet? I would like to test this but I don't even know how to return the Path and Name for the Excel temporary file.
Public Sub LocalJetQuery()
Dim objStartingRange As Range
Dim objConnection As New ADODB.Connection
Dim objRecordset As New ADODB.Recordset
Dim strDSN As String
Dim strSQL As String
Set objStartingRange = Application.Selection
If CLng(Application.Version) >= 12 Then
strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
& "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";"
Else
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
& "Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
End If
strSQL = "SELECT * FROM [" & objStartingRange.Worksheet.Name & "$];"
objConnection.Open strDSN
objRecordset.Open strSQL, objConnection
Application.Workbooks.Add(xlWBATWorksheet).Sheets(1).Cells(1, 1).CopyFromRecordset objRecordset
End Sub
Thanks!
No. Just like David Fenton says in the comments.

Resources