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)
Related
I am writing a small application in Excel 2002 and I need to store numbers in some format, it can be a string.
The tables I have a 1:1 relationship and other table is just a table of one column so using access is not necesary and having to have another file is something I'd like to avoid.
So, I want to store it in separate sheets.
However, I like the benefits of SQL for querying and I need it.
I tried using some ADODB connection strings to reach this but I cannot achieve it.
I used the following code:
Dim cn As Object, rs As Object, output As String, sql As String
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=1"
.Open
End With
Also, do I have to use ODBC or should I use OLE DB? I don't know if OLE DB could be used to query in excel files.
Also, is it possible to do inserts with SQL using this ODBC or OlE DB?
I tried different providers in the connection string, and I checked the ADO references to be available.
Also, I get this error:
"Error 3706. The specified provider could not be found. It may not be installed properly."
Connection issue
First, there was an error in your Provider string, it should not contain the part with Data Source=C:\MyExcel.xls; since this is part of the connection string. So it should look like this:
.Provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=1"
ODBC vs OLEDB
I've never used ODBC, but based on this answer, you can't use it to query an Excel file, so OLEDB is the way to go.
Insert Statement
Once you have a working ADODB connection, insert query should work as hoped. I'm providing an example below that worked for me, but there is a few caveats:
I'm using the ACE.OLEDB.12.0 instead of JET.OLEDB.4.0 with Excel for Microsoft 365 MSO (Version 2112 Build 16.0.14706.20000) 64-bit on Windows 10.
I'd suggest to set Mode=ReadWrite in your connection string to avoid potential writting permission issues (but it might work even without it.).
Regarding the IMEX setting, I was having errors when it was set to IMEX=1, so I switched to IMEX=0 (see related question.
The example
With a workbook named Data.xls with the first sheet named Data and the following data :
Data for copy-paste
I can run the following:
Dim wb As Workbook
Set wb = Workbooks("Data.xls")
Dim ws As Worksheet
Set ws = wb.Worksheets("Data")
'Create connection
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0;"
.ConnectionString = "Data Source=" & wb.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=0;Mode=ReadWrite;"
.Open
End With
'Compose the INSERT statement.
Dim query As String
Const sep = ", "
query = "INSERT INTO [" & ws.Name & "$] " & _
"(Id, Name, Age) " & _
" VALUES (" & _
4 & sep & _
"'" & "Joe" & "'" & sep & _
40 & _
")"
'Execute the statement.
conn.Execute query, adCmdText
'Close the connection
conn.Close
And it should insert the data as follow:
Should you use ACE or JET?
If JET works for you, you might as well use it. Based on this article , you should also have the 32-bit version of ACE available with Windows 7 to work with Excel 2002 (32-bit), but based on your comment it seems like it's causing some problems.
See also some interesting answer about JET vs ACE.
I have recently upgraded my version of excel from Excel 2016 to Excel 365.
I have a VBA code which makes a drop down list more dynamic by running a sql query from a dataset in the same Excel workbook. This code used to work in Excel 2016 but it no longer works in the upgrade:
cn_xl.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 12.0; HDR=Yes"";"
The error is: Cannot update. Database or object is read-only.
The wider context of this code is:
'Connect to the campaign data stored in excel
Dim cn_xl As ADODB.Connection
Set cn_xl = New ADODB.Connection
cn_xl.Open "Provider=Microsoft.ACE.OLEDB.16.0; Data Source=" & ThisWorkbook.FullName & "; Extended Properties=""Excel 16.0 XLSM; HDR=Yes"";"
Dim sql_xlcm As String
sql_xlcm = "select media_type, timeperiod, control, exposed, uplift, uplift_pct " & _
"from [Campaign_data$A1:I" & s05_cmpgn_data.Cells(Rows.Count, 1).End(xlUp).Row & "] " & _
"where product = 'Offer' " & _
"and metric = '" & ThisWorkbook.Sheets("Customer Averages").DropDowns("Drop Down 6").List(ThisWorkbook.Sheets("Customer Averages").DropDowns("Drop Down 6").ListIndex) & "';"
Dim rs_xlcm As ADODB.Recordset
Set rs_xlcm = New ADODB.Recordset
With rs_xlcm
.ActiveConnection = cn_xl
.Open sql_xlcm
ThisWorkbook.Sheets("Customer Averages").Range("E21").CopyFromRecordset rs_xlcm
.Close
End With
What can I do to fix this?
Thanks
Microsoft ACE.OLEDB provider, version 12.0...16.0 is required.
Office 365, as i know, does not have this provider.
And full connection string is:
"Provider=Microsoft.ACE.OLEDB.16.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=Yes';"
where {0} your Excel file
Most probably you have to install:
Download Microsoft Access Database Engine 2016 Redistributable from https://www.microsoft.com/en-us/download/details.aspx?id=54920
Another sample and additional information available on: https://github.com/KohrAhr/SqlOverExcel/wiki
thx to google I figured out a lot of weird tricks you can do to connect to simple SQL server or SAP database and such. However, no matter how hard I try I can not create odbc object.
I can pull odbc database to excel, but this is not what I want to do.
I want to use INSERT and DELETE SQL queries.
The simplest line of codes I found were these:
Private Sub ConnectDB()
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & "SERVER=******.****.**;" & "DATABASE=testServer;" & "USER=Mikk;" & "PASSWORD=**;" & "Option=3"
End Sub
I have ticked the box for ActiveX data object 2.8 Library
I get run-time error '-2147467259 (80004005)
Automation error
Unspecified error
Money solves problems :)
I didn't use the correct database name and did not use DSN.
Also using a specific driver was not a good idea.
Even the server aadress was not needed.
Here is the solution:
Sub test_connection()
Dim oConn As New ADODB.Connection
Dim strUsername As String
Dim strPassword As String
Dim strDatabase As String
strUsername = "Mikk"
strPassword = "****"
strDatabase = "test"
oConn.Open "DSN=testServer;" & _
"Database=" & strDatabase & ";" & _
"Uid=" & strUsername & ";" & _
"Pwd=" & strPassword
oConn.Close
Set oConn = Nothing
End Sub
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!
I am using Excel 2010 with a macro to access another daily spreadsheet to pull data for forming an FTP file of records. The specific problem I am having is a runtime connection error. The error I am getting is '-2147467259(80004005)': Unrecognized database format 'C:\Work\Daily FTP Process\Excel DBs and Files\ftp.xlsx'. All I need it to know where to look. Here is the connection string from the watch:
: ConnectionString : "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Work\Daily FTP >Process\Excel DBs and Files\ftp.xlsx;" : String : Module1.XLFixedFieldFile
Here is the pertinent (or impertinent) code:
Dim conn As Object
Dim cmd As Object
Dim psidRecSet As Object
Dim loopIndex As Long
Dim connString As String
Dim sqlString As String
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
connString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & XLName & ";"
conn.Open connString <==== Here is the line where it is breaking
SOLVED! The connectionstring that worked is as follows:
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & XLName &
"';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
Remou, I had tried that, but had something wrong and got an error. The copy&paste of a working line found in another forum was my salvation. I appreciate the help.
For others using this yto solve their problems, to work with Excel 2010, I went to "Tools/Reference" and enabled Microsoft AcriveX Data Objects 6.1 Library and Microsoft ActiveX Data Objects Recordset 6.0 Library.
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & XLName &
"';Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"
conn.Open connString
You want:
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& xlFile & ";Extended Properties='Excel 12.0 Xml;HDR=No;IMEX=1';"
Note the quotes for the extended property.
You might like to read http://support.microsoft.com/kb/257819, with particular reference to IMEX.