-
Mar 14th, 2017, 03:55 PM
#1
Thread Starter
Fanatic Member
ADODB with Excel
Hello:
I know I've done this before. I am trying to use Excel to read in data for use in another Application using ADO.
the
In the function below, OLDPART and NEWPART represent column names.
The whole things hands when it cannot find the connection at cn.Open (cnstr).
I have not assigned anything for dsn in the control panel, because I thought the program took care of this. Is that not correct?
I am using a 32-bit version of Office 365 (2016). I have tried both connection strings for the variable cnstr.
Reference is Microsoft.ActiveX Data Objects 6.1 Library. I also tried 2.8.
Code here: What am I missing? The error is: System.Runtime.InteropServices.COMException: 'Provider cannot be found. It may not be properly installed.'
Code:
Private Sub GetADOfromExcel(ByVal ExcelFile As String, ByVal OldPart As String, ByVal NewPart As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
cn = CreateObject("ADODB.Connection")
rs = CreateObject("ADODB.Recordset")
Dim cnstr As String = “Provider=Microsoft.ACE.OLEDB.4.0;Data Source=” & ExcelFile & “;Extended Properties=Excel 12.0;HDR=Yes;IMEX=1;"
' Dim cnstr As String = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & ExcelFile & ";HDR=Yes';"
cn.Open (cnstr)
Dim qry As String = "SELECT " & OldPart & ", " & NewPart & "FROM [Sheet1$]"
rs.Open(qry, cn, ADODB.CursorTypeEnum.adOpenStatic)
Dim recnum As Integer = 1
rs.MoveFirst()
Do Until rs.EOF
MsgBox(rs!oldpart & ", " & rs!newpart)
rs.MoveNext()
Loop
rs.Close()
cn.Close()
End Sub
-
Mar 14th, 2017, 05:14 PM
#2
Thread Starter
Fanatic Member
Re: ADODB with Excel
I won't say that I've made huge progress, but I was able to get the error to change to System.Runtime.InteropServices.COMException: 'Could not find installable ISAM.'
Please provide any quidance you can. By the way, 32-but version of Office running on Windows 7-64...
Code:
Private Sub GetADOfromExcel(ByVal ExcelFile As String, ByVal OldPart As String, ByVal NewPart As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
cn = CreateObject("ADODB.Connection")
rs = CreateObject("ADODB.Recordset")
Dim cnstr As String = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” & ExcelFile & “;Extended Properties=Excel 12.0;HDR=Yes;"";"
' Dim cnstr As String = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & ExcelFile & ";HDR=Yes';"
cn.Open (cnstr)
Dim qry As String = "SELECT * FROM [Sheet1$]"
rs.Open(qry, cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockOptimistic, CommandTypeEnum.adCmdText)
Dim recnum As Integer = 1
rs.MoveFirst()
Do Until rs.EOF
MsgBox(rs!oldpart & ", " & rs!newpart)
rs.MoveNext()
Loop
rs.Close()
cn.Close()
End Sub
-
Mar 15th, 2017, 03:21 PM
#3
Re: ADODB with Excel
i have read somewhere that regardless of the excel version, with jet, you should use excel 8.0, as this refers to the driver version, not the excel version
with ace, that may be different
is excelfile a full path to the file?
extended properties need to be enclosed in "" like
Code:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ExcelFile & ";Extended Properties=""Excel 12.0;HDR=Yes;"";"
OR
Code:
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties='Excel 8.0;hdr=yes';"
.Open
End With
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 15th, 2017, 04:26 PM
#4
Thread Starter
Fanatic Member
Re: ADODB with Excel
Great solution for old-school xls files. If anyone knows what to use for xlsx files, I'd love to know about it! Excel 12, 8, etc., does not seem to matter.
Code:
' xls
cn.Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & ExcelFile & "; Extended Properties = ""Excel 8.0;HDR=Yes""; ")
' xlsx
cn.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " & ExcelFile & "; Extended Properties = ""Excel 8.0 Xml;HDR=Yes""; ")
-
Mar 16th, 2017, 03:04 AM
#5
Addicted Member
Re: ADODB with Excel
Hello,
I'm using this connection string:
Code:
strCS = "Provider=Microsoft.ACE.OLEDB.12.0;"
strCS = strCS & "Data Source=" & strPath & ";"
strCS = strCS & "Extended Properties=" & Chr(34) & "Excel 12.0 Macro;"
strCS = strCS & "HDR=YES;" & Chr(34) & ";Persist Security Info=False"
and also chech in you registry (regedit) if you have this key:
Code:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.ACE.OLEDB.12.0
if not, then install the driver:
https://www.microsoft.com/en-us/down....aspx?id=13255
When I was using
Code:
"Provider=Microsoft.Jet.OLEDB.4.0;"
it gives me error: could not find installable isam
My projects:
Virtual Forms
VBA Telemetry
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|