Read Excel file into ADODB recordset - excel

I have an Excel VBA application that reads information from other excel files using ADODB recordsets. This application was working just fine, but a company software update just broke it (I guess, can't explain otherwise).
Copied below is a stripped down version that replicates the error I'm getting (also shown below). I've tried to review the many posts that reference Microsoft.Jet.OLEDB.4.0, but I'm not sure this is related. They all reference a specific error that I'm not seeing.
The identified error is caused by the CN.Open command. Thanks to comment from Matt'sMug, I have determined that I can execute the connection with no issue, provided the subject workbook is already open in Excel. If it is closed (as it should be), the error returns. Any ideas?
Public Sub GetExcelContent()
Dim Excelbook As String
Dim CN As ADODB.Connection, RS As ADODB.Recordset
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Excelbook = Sheets(1).Cells(4, 3)
Set CN = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= " & Excelbook & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
RS.Open "SELECT * FROM [M2_F$]", _
CN, adOpenStatic, adLockOptimistic, adCmdText
End Sub

So, I have not figured out why the previous provider has ceased to work. However, I was able to switch to an alternate that works just as well. The coding is a little cleaner as well, so I'm happy. Here's my new protocol:
Dim CN as New ADODB.Connection
Dim RS as New ADODB.Recordset
Cstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Excelbook & _
& "; Extended Properties='Excel 12.0 Xml;HDR=YES';"
CN.Open Cstring
RS.Open "SELECT * FROM [M3_F$]", CN, adOpenStatic, adLockOptimistic, adCmdText

Related

What is the alternative for adodb.recordset to access excel worksheet?

I used to using ADODB.Recordset to operate worksheet with "Microsoft ActiveX Data Objects 6.1 Library".
For example:
Function Sampe()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
sql = "SELECT * FROM [Sheet1$]"
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES"";"
Set rs = conn.Execute(sql)
While Not rs.EOF
'process data
rs.MoveNext
Wend
conn.Close
End Function
This code works fine in windows but would occur error "Can't find project or library" on macOS Excel 2019.
Looks like macOS didn't support ActiveX dll.
Is there any other way to use sql to operate workSheet data without ADODB?

How to properly make a query for excel worksheets using ADO?

Below is the data I am trying to query:
I ran the code that was found in this documentation in this microsoft documentation as shown below:
Sub query()
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\Sun-Lap\Desktop\Sample (Payroll).xlsm; Extended Properties=""Excel 8.0; HDR=YES"";"
objConnection.Open connectionString
objRecordset.Open "Select * FROM [DailyTimeRecord$] Where Person = JOHN", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF
Wscript.Echo objRecordset.Fields.Item("Date"), _
objRecordset.Fields.Item("Person")
objRecordset.MoveNext
Loop
End Sub
On Error Resume Next causes my application to infinitely run and hangs my application.
I tried replacing Microsoft.Jet.OLEDB.4.0 to Microsoft.ACE.OLEDB.12.0 based on a suggestion. I also tried to replace Extended Properties=""Excel 8.0; HDR=YES""; to Extended Properties=""Excel 12.0 Macro; HDR=YES"";
But I still get an error on objRecordset.Open "Select * FROM [DailyTimeRecord$] Where Person = JOHN", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
I get an Runtime error: No value for given for one or more required parameters

Microsoft.Ace.OLEDB.16.0

Can anyone please help me understand why Microsoft.Ace.OLEDB.16.0 doesn't work on Windows 10 x64 with Office 2016 x86?
I get runtime error -2147467259 (80004005) when the connection to OLEDB 16.0 is opened using the following code:
Public Sub ValidityDateCheck_SRT_Templates()
NameFile = "x"
sConn = "Provider=Microsoft.ACE.OLEDB.16.0;WSS;IMEX=1;RetrieveIds=Yes;" & _
"DATABASE=" & sSHAREPOINT_SITE & ";" & _
"LIST=" & sDEMAND_ROLE_GUID_ML & ";"
Set cn = New ADODB.Connection
Set rst = New ADODB.Recordset
With cn
.ConnectionString = sConn
.Open '--> **HERE I GET THE ERROR**
End With
Rs = "SELECT * FROM [Template Library];"
rst.Open Rs, cn, adOpenStatic, adLockOptimistic
Do Until rst.EOF
If Left(rst![Name], InStr(1, rst![Name], "#") - 1) = TemplateID Then
NameFile = rst![Name]
On Error GoTo Skip
ActualVdate = rst![Validity Date]
TransVdate = rst![Transition Period End]
Exit Do
Else
rst.MoveNext
End If
Loop
Skip:
rst.Close
The interesant part is that in DEBUG, if I use 12.0 first and then change to 16.0 and save, it will open it. Then, if I reopen file, i get same error.
Please help me guys.
Are you using VBA? If so, please ensure you have enabled the Microsoft ActiveX Data Objects 6.1 Library on the reference menu.
Try this;
cn.Open "Provider=Microsoft.Ace.OLEDB.16.0;" & _
"Data Source=" & UserForm2.TextBox32.Text & ";Jet OLEDB:Database Password=1101010;Persist Security Info=False;"

Write/update data via ADO to closed Excel workbook

I'm trying to change the number in a closed Excel workbook using ADO. I found the below code written in 2015 by L42. But the procedure stops with the error "No value given for one or more required parameters". I can't find where something is missing!
Sub ChangeNum()
Dim con As ADODB.Connection, rec As ADODB.Recordset
Dim sqlstr As String, datasource As String
Set con = New ADODB.Connection: Set rec = New ADODB.Recordset
datasource = "D:\DropBox\TraderShare\TraderNum.xlsx"
Dim sconnect As String
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & datasource & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
con.Open sconnect
sqlstr = "UPDATE [Sheet1$] SET [Number] = ""16900"" WHERE [ID] = INum"
rec.Open sqlstr, con, adOpenUnspecified, adLockUnspecified 'adLockOptimistic , adOpenStatic, adLockReadOnly
con.Close
Set rec = Nothing: Set con = Nothing
End Sub
Grateful for any help!
I found the fault! The sqlstr should be written:
sqlstr = "UPDATE [Sheet1$] SET [Number] = ""16900"" WHERE [ID] = ""INum"""
It's the exclamation marks that matters! I don't know why it's required in this way, if the exclamation marks are required to concatenate different parts of the string. But they look to be out of sequence, there's 1 too much in the end!
The usual reason for this error is a missing or misspelled value. I would go over the SQL statement specifically and see whats off

asp and ms-access db - how to import data from xls file

i want to allow the user to upload xls file with 9 columns and unlimited number of rows.
i will run over everyline and insert the data to the db
how do i read the xls file?
You can read the XLS by opening an ADO recordset which pulls in the spreadsheet's data.
This example reads data from a spreadsheet named Billing Summary which includes column names in the first row..
Public Sub ReadSpreadsheet()
Const cstrFolder As String = "C:\Access\webforums"
Const cstrFile As String = "ExampleFinance.xls"
Dim strConnect As String
Dim strSql As String
Dim cn As Object
Dim rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
cstrFolder & Chr(92) & cstrFile & _
";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strConnect
strSql = "SELECT * FROM [Billing Summary$] WHERE SomeField Is Not Null;"
rs.Open strSql, cn
Do While Not rs.EOF
'* do something with each row of data *'
'Debug.Print rs!SomeField '
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
If that particular connection string doesn't work for you, look at other examples of Excel connection strings at Connection strings for Excel
Edit: That example works in Access. But you said ASP. I think it will work there, too, if you drop the data types from the variable and constant declarations: Dim strSql instead of Dim strSql As String
Example of using an SQL statement to update Access from Excel.
Set cn = CreateObject("ADODB.Connection")
scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\dbto.mdb"
cn.Open scn
sSQL = "SELECT * INTO NewTable FROM "
sSQL = sSQL & "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=C:\Docs\From.xls].[Sheet1$]"
cn.Execute sSQL, recs
MsgBox recs
In C#, I had to load an excel spreadsheet to a DataSet - this got me there...
Code Project Example
I used Option 1 - the Preferred method! Hope this helps...
Mike

Resources