Error opening recordset in VBA - excel

I'm developing an Excel VBA application using ADO. I'm trying to open a recordset, but the open method fails if my table has more than 65536 lines. I know this number is the old Excel line limit, but I'm using the 2016 version and the correct connection strings. Perhaps it's some library referenced in my project, but I can't find out which one.
I would appreciate very much if I could get some help with this.
The error:
Runtime error '-2147217865 (80040e37)': The Microsoft Jet database engine could not find the object 'My sheet$A8:AD70000'. Make sure the object exists and that you spell its name and the path name correctly.
My code (I had to switch the worksheet name so I don't reveal any sensitive data):
Sub MySub()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Set rs = New ADODB.Recordset
Set conn = New ADODB.Connection
str = "SELECT * FROM [My sheet$A8:AD70000];"
'Opening connection with the workbook
conn.ConnectionTimeout = 90
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1"";"
conn.Open
rs.Open str, conn, adOpenForwardOnly, adLockReadOnly, adCmdText
rs.Close
conn.Close
End Sub
If I change "My sheet$A8:AD70000" to "My sheet$A8:AD60000", this code Works.
The libraries I'm using:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Office 16.0 Object Library
Microsoft ActiveX Data Objects 6.1 Library
OLE Automation
Thank you very much for your attention.

Related

Excel VBA - runtime error 80004005 after server migration

Recently my company just performed a server migration and now one of my Excel VBA is not working,
Here's my code snippet:
Dim strSQL As String, conStr As String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
'On Error GoTo exitsub
Path = ActiveWorkbook.Sheets("Options").Cells(1, 6)
conStr = "Provider=Microsoft.ace.OLEDB.12.0;Data Source='" & Path & "';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"
strSQL = "SELECT * From [MASTER]"
cnn.Open ConnectionString:=conStr 'stuck at this line
rs.Open strSQL, cnn, adOpenDynamic, adLockReadOnly
The place where I'm stuck at is when its trying to perform the SELECT query. The macro triggers when I save the file but doing so only give me this error:
I've checked my reference for Microsoft ActiveX Data Object, and confirmed that the latest one I have (16.0) is selected.
How do I fix this?
Problem
Server-side automation of office is not supported https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2
Possible workaround
Do it with .Net https://stackoverflow.com/a/52050257/495455
Possible solution/workaround
https://knowledge.informatica.com/s/article/107777?language=en_US
Solution
This is as designed behavior from Microsoft for the Excel driver which currently does not have feature to read from a password protected Excel file, unless it is open.
Workaround
Open the Excel file whose definition is being imported and provide the correct password and try to import the definition from Designer.

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 connect to Oracle 12c DB with VBA

I have added a reference to Microsoft ActiveX Data Object 2.0 Library, and tried to execute the following code:
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=****)(PORT=****))" & _
"(CONNECT_DATA=(SERVICE_NAME=****))); uid=****; pwd=****;"
con.Open (strCon)
End Sub
I am getting these errors:
I have tried installing Oracle instant client ODBC drivers (I've tried 32bit and 64 bit versions) and I am having no luck.
I am not sure what the problem is. I can connect to the database in Visual Studio 2012 with VB.Net, but I am unable to connect in excel 2010 using VBA.
Any insight would be greatly appreciated!

VS2010, Windows Form Application

As part of the application, I am trying to update an excel sheet using ado. But I get an error stating "ODBC driver does not support the requested properties".
Code is as below. Can anyone please suggest . Thanks.
Dim cn As New ADODB.Connection
Dim rc As New ADODB.Recordset NewNmae = "INSERT INTO [Names$] VALUES('ASDASD','ASDASDASD','ASDASDASD');"
cn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
"DBQ=" & ListNames
cn.Open(cn.ConnectionString)
rc.Open(NewNmae, cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic)
cn.Close()
rc.Close()`
You should set the provider to Jet OLEDB:
cn.Provider="Microsoft.Jet.OLEDB.4.0";

Unspecified run time error while executing vba script

I am new to vba and I am using vba script to connect to database from excel and get the records. I have written the following script for that.I am getting a run time error '-2147467259(80004005)':Unspecified error.
How to resolve this error. See the error screen shot.
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
'--- Replace below highlighted names with the corresponding values
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=host_name)(PORT=1521))" & _
"(CONNECT_DATA=(SERVICE_NAME=service_name))); uid=id; pwd=pw;"
'--- Open the above connection string.
con.Open (strCon)
'--- Now connection is open and you can use queries to execute them.
'--- It will be open till you close the connection
query = "select * from security.forms"
Set rs = con.Execute(query)
For i = 0 To rs.Fields.Count - 1
Sheet1.Cells(1, i + 1).Value = rs.Fields(i).Value
Next
con.Close
End Sub
Error screen shot:
Had you tried to use the open method of your RecordSet instance? Maybe it will give you another error that will be more helpful.
Dim connection As New ADODB.connection
Dim rst As New ADODB.Recordset
Dim query As String
connection.ConnectionString = CONNECTION_STRING
connection.Open
rst.Open query, connection, adOpenKeyset, adLockOptimistic
do while not rst.EOF
rst.MoveNext
loop
connection.Close
I had the exact same problem and it was tough to find an answer since most posts on this issue are unanswered.
I solved it by using another Oracle driver. Instead of Microsoft ODBC for Oracle try using the Oracle in OraClient11g_home1 driver. Hope this helps
Please add the reference 'Microsoft ActiveX Data Objects 2.8 Library'

Resources