Reading excel with the driver xlsx vb6 - excel

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

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.

Excel VBA macro crashing when opening ADODB MS Access Recordset

I have a problem whereby I have an excel vba macro which works perfectly on my local machine however when stored on a server crashes.
I have identified the point at which the macro crashes, and this is when the excel macro attempts to open the ADODB Recordset (Db is MS Access).
The folder structure in the code is identical to my desktop setup so I don't think this is the issue. Just seems to be when the excel file attempts to query the database when opening the ADODB recordset.
Any help much appreciated! If you need further info please let me know.
Thanks in advance!
EDIT:
Upon further investigation, I have verified opening and closing both the connection and recordset as working fine.
Its when I pass this query through as the source which causes excel to crash. Here is my code:
Dim DirectoryLocation As String
DirectoryLocation = Application.ActiveWorkbook.Path
Dim ConnString As String
ConnString = _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DirectoryLocation & "\Data\UK Sales.accdb;Persist Security Info=False;"
'Declare new database connection object
Dim DBConn As ADODB.Connection
'Set new database connection object
Set DBConn = New ADODB.Connection
'Declare new recordset object
Dim SalesData As ADODB.Recordset
'Set new recordset object
Set SalesData = New ADODB.Recordset
'Define connection string property of database connection object
DBConn.ConnectionString = ConnString
'Open database connection
DBConn.Open
'On Error GoTo CloseConnection
'Set parameter values for reporting
Dim Rep_Param As String
Rep_Param = Application.WorksheetFunction.VLookup(Worksheets("Dashboard").Range("C5"), Worksheets("Lookups").Range("H:I"), 2, 0)
Dim Rep_Option As String
Rep_Option = Worksheets("Dashboard").Range("C6").Value
'Get customer names for report and populate report
'------------------------Query---------------------'
Dim Cust_Billing_Qry As String
Cust_Billing_Qry = "SELECT DISTINCT Customer_Billing FROM Sales WHERE " & Rep_Param & "='" & Rep_Option & "'"
SalesData.Open Cust_Billing_Qry, DBConn
'On Error GoTo CloseRecordSet
Sheets("Revenue Analysis (Detail)").Range("C:C").ClearContents
Sheets("Revenue Analysis (Detail)").Range("C6").CopyFromRecordset SalesData
SalesData.Close
Any help much appreciated!

ADO Connection conflicting with other excels (very weird issue)

I created quite a comprehensive End User application in VBA excel the last two months which automated the whole reporting chain based on a daily dump of data in an excel sheet.
All worked fine until the tool went live. When launching my code on a operation desk, sometime the queries on the data dump returns no results.
This only happens when the tool was not opened as a first excel instance.
So, when another excel tool is open first, my tool:
- Returns no results
- An another instance of my tool is opened in read-only
This happens as well on windows 2007 & 2010 and on Win XP & Windows 7.
It opens the other excel on following line (where the issue is situated:
'adoConn.Open sConnString'
Below my code:
Public Function createConnection() As ADODB.Connection
Dim DbPath As String
Dim sConnString As String
Dim adoConn As New ADODB.Connection
DbPath = ThisWorkbook.FullName
'Define connection String
'http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c19307/Whats-in-an-ADO-Connection-String.htm
sConnString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DbPath & ";HDR=Yes';"
'Open the connection
adoConn.Open sConnString
'Return the connection
Set createConnection = adoConn
End Function
The connection string you're using isn't the standard Excel connection string for ADO. Replace:
sConnString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DbPath & ";HDR=Yes';"
adoConn.Open sConnString
with the following:
With adoConn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & dbPath & ";" & _
"Extended Properties=""Excel 12.0 Macro;HDR=Yes;IMEX=1"";"
.Open
End With
I rarely have issues with querying open workbooks with ADO (apart from occasional phantom read-only copies appearing)

Connection to server failed from VBA but works from Access

I have setup some queries in MS access that fetch data from a MS SQL server on the network. The queries work fine when I run them from MS access. However, when trigger the same query through VBA in Excel (need of my dashboard), then it says ODBC connection failed. I am not able to figure out what is broken in the process. All details below:
ODBC connection string in MS Access - set in the properties of the query
ODBC;DRIVER=SQL Server;SERVER=xyz;UID=xyz_reader;PWD=xyzpassword;Network=DBMSSOCN;Address=xyz,1433
Connection string used in Excel VBA - this is used to trigger the odbc query in ms access
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & mydb.accdb
Error that I get
Run-time error '-2147467259 (80004005)':
ODBC--connection to 'SQL Serverxyz' failed.
Excel VBA Connection is an ADODB connection so you need to add a references Tool>>References then add “Microsoft ActiveX Data Object Library”
Then use code like
Dim strConn As ADODB.Connection
Dim rs As ADODB.Recordset
Set strConn = New ADODB.Connection
Set rs = New ADODB.Recordset
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=YourDATABASE;Data Source=YourServer"
strSQL = "Your Select statement”
strConn.Open
rs.Open strSQL, strConn
Now you have your values in record set. I hope this will solve your issue.

Resources