VS2010, Windows Form Application - excel

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";

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.

Error opening recordset in VBA

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.

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!

Reading excel with the driver xlsx vb6

I'm having a problem during one week, and I couldn't find the solution. I'm trying to read a file in xlsx on vb6 as a component of the asp classic.
First issue that I couldn't be able to solve was this
Dim cn As New ADODB.Connection
Dim provider As String
provider = "Driver={Microsoft Excel Driver (*.xlsx)};DBQ=" & nomArq & ";"
Set cn = CreateObject("ADODB.Connection")
cn.Open provider
The error that I got in this connection was
"[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
The second try was in this way
Dim cn As New ADODB.Connection
'Dim provider As String
With cn
.provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & nomArq & ";" & ";Extended Properties=\""Excel 8.0;HDR=Yes\;"""
.Open
End With
And the erros was
"Format of the initialization string does not conform to the OLE DB specification."
I also tried in another way and I got the error:
"could not find installable isam"
but I don't have the code hear anymore.
someone could help me, I looked in all most everything on google
I don't really understand what you are trying to do but it seems like you are trying to establish a connection (looking at your code).....try something like this with what you are doing....
Dim g_cnDB as ADODB.Connection
Set g_cnDB as ADODB.Connection
g_cnDB.ConnectionString = "Provider=TEXT;User ID=TEXT; Pwd=TEXT; Initial Catalog=TEXT; Data Source=TEXT"
'Then set it's timeout to be unlimited, so your connection remains open until you close it
g_cnDB.CommandTimeout = 0
'Establishing the connection
g_cnDB.Open

need to read xls files to import data, using ADODB.Connection having System resource exceeded

Hi I'm trying to read a excel file from our web site to importe data to our SQL.
it's working great until this morning on the line
objConn.Open "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & path & vattachid &"."&GetFileExtension(filename) & "; ReadOnly=1;"
I received this error
Error Number -2147024882
Error Code
Error Description [Microsoft][ODBC Excel Driver] System resource exceeded.
ASP Description
Category Microsoft OLE DB Provider for ODBC Drivers
If I reboot the server it's working fine again...
what I use is this :
Sub ReadExcel(vvalid)
Dim objConn, objRS, strSQL
Dim x, curValue
DIM vSumQty
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & path & vattachid &"."&GetFileExtension(filename) & "; ReadOnly=1;"
strSQL = "SELECT * FROM A1:AZ1000"
Set objRS=objConn.Execute(strSQL)
/// validating and saving the data
objRS.Close
Set objRS=Nothing
objConn.Close
Set objConn=Nothing
END SUB
Any input on what I'm doing wrong ?
Is the excel workbook open when you try to get the recordset from it? There is a known Microsoft bug that causes a memory leak if it is. See here http://support.microsoft.com/kb/319998

Resources