I am reading data from an Excel 2007 spreadsheet using ADO. Setting up the connection is easy:
Dim ado As ADODB.Connection
Set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFilename.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=NO;IMEX=1"";"
ado.Open
I can call ado.OpenSchema without any trouble on this object. However, when I try to query the data:
Dim rs As ADODB.recordSet
Set rs = ado.Execute("SELECT * FROM [Current Work Load$]")
I simply get a table full of Nulls.
This is mentioned as an issue on the Microsoft Support site - but I have explicitly enabled "Import Mode" (as you can see in the code above - IMEX=1).
The Execute method does not return any records as it is for action queries.
Your might want to try the OpenRecordset method.
Dim rs As ADODB.recordSet
Set rs = ado.OpenRecordset("SELECT * FROM [Current Work Load$]")
I've found the ADO connection strings here are unbelievably picky. I've gotten reading the spreadsheets to work but with a slightly different connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + #";Extended Properties="Excel 12.0;IMEX=1";
(I don't have the XML after the Excel 12.0 declaration).
SpreadsheetGear for .NET can read Excel workbooks and enables you to access any cells without the kinds of issues / limatations you can run into with ADO.
You can see live C# & VB samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC
As well as using IMEX=1 in the connection string, you need to review a couple of registry keys. For more details, see this answer on SO.
Related
I use Excel VBA with SQLite3 ODBC driver obtained from http://www.ch-werner.de/sqliteodbc/
Both are 32 bits version on a Windows 10 computer.
A simple Select query on a single table with 16 columns and 200 rows takes approximately 200ms per row (I timed it).
This is more than 30 seconds for a query that executes <30ms in SQLite3.
I tried several cursorType , LockType , CursorLocation with no success
I tried Excel 2010 and Excel 365.
Sub TestSqlite()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
cnn.Open "DRIVER=SQLite3 ODBC Driver;Database=C:\Test.sqlite;"
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockReadOnly
rs.Open "SELECT * from SimpleTable limit 20;", cnn
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub
Is there any way to fix this ?
There is a known problem with this stupid (pardon my French) Microsoft Antimalware Scan Interface (AMSI) feature, which sounds like a complete BS to me, and which grossly degrades performance of VBA7 DLL calls (I tried a few suggested on the Internet workarounds, but nothing worked for me). While when using ADO you don't call a DLL directly, eventually it calls the SQLiteODBC driver, and AMSI is the most likely culprit (together with all Windows built-in and activated by default security features). I have conducted a few tests of my own last year, though in a slightly different environment. Even though I had disabled most security features, the result was pretty astonishing.
I am trying to use Excel as Database in Automation Anywhere 10. Following is my code
Connect to "Provider=Microsoft.ACE.OLEDB.16.0",Data Source = C:\Users\myuser\Documents\demo.xlsx;Extended Properties="Excel 10.0 Xml;HDR=YES";"Session:"session1"
Execute SQL statement:'Select * from [Sheet1]$'
But it is showing the following error,
Provider cannot be found. It may not properly installed.
Can anyone help on this?
Try
dim cn as object, cnstr as string, rs as object
set cn = createobject("adodb.connection")
cnstr = "Provider=Microsoft.ACE.OLEDB.16.0,Data Source=C:\Users\myuser\Documents\demo.xlsx;Extended Properties=""Excel 10.0 Xml;HDR=YES"";Session:""session1"""
set rs = cn.Execute("Select * from [Sheet1]$")
some version EXCEL use Provider=Microsoft.ACE.OLEDB.12.0 instead of Provider=Microsoft.ACE.OLEDB.16.0.
Couple of things you can check
You may not have the database engine actually installed for the Office suite. I would check this first especially if you've not been able to connect before. Here is a link. This requires Access be installed, I think.
You may also try Provider=Microsoft.ACE.OLEDB.12.0 or Extended Properties="Excel 12.0;HDR=YES"; as I do not think you need to the XML in Excel 10.0 XML.
I have two additional notes for when you get it working:
You need to reference your table name like so [$vSheetName$$] with the $ inside the brackets. Two dollar signs like so if you're inputting a variable
It is a best practice to wrap your headers in ticks [ ` ] seen between the brackets. With AA, if any header has spaces, this is the only way to parse it
Your query syntax is incorrect.
Try using the following syntax instead:
Select * from [Sheet1$]
I want to run a MS Access query from Excel VBA. For that I'm using ADODB.Connection and ADODB.Command to call the query. In principle it works, but there is an issue with the name, because it contains slashes: "Query_3/6/1/1". Running the script below, I get an error message like "The Microsoft Office Access database engine cannot find the input table or query 'Query_3'." So it doesn't consider the rest of the name following the slash. I already tried escaping it with brackets [], but it doesn't help and other than that I didn't find a solution.
Renaming the module works, but there are lots of them and there are already other dependendies, so that is not really a solution.
I'm very glad for any help!
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open "C:\Users\...\file.accdb"
End With
With cmd
.ActiveConnection = con
.CommandText = "Query_3/6/1/1"
.CommandType = adCmdStoredProc
End With
Set rs = New ADODB.Recordset
rs.Open cmd
...
I don't think you are going to like the answer. I did some testing and Excel is just not going to honor that naming convention. I even called a query built from that Query_3/6/1/1 and it would not work. I encountered this same issue with queries that used Nz() function. Had to redesign queries.
Only alternative I can see is a procedure in Access that writes the query records to a 'temp table' (table is permanent, records are temporary) and the Excel calls this procedure then opens recordset based on the temp table.
I've stumbled accross a problem I am unable to solve :(
Neither Google nor StackOverflow have given me any usable answers, so I'm turning to you.
The problem is this:
I've created a spreadsheet that loads data from an Access database stored on a network drive.
The data-loading part is only done once, i.e. when opening the file.
I open the connection like this:
Dim con As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Set con = GetConString()
rs.Open "SELECT ID, somevalue FROM sometable", con
Where to connection string is something like this
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & (network path to file) & "'"
Then I dump the information in the spreadsheet and terminate the connection like this:
rs.Close
con.Close
However, when I try to hibernate the PC while this Excel spreadsheet is still open, I get an error message.
It translates to something along the lines of "Excel has prevented the computer from going to sleep".
This seems to happen only when using this constellation...
Does anyone have any idea on how to prevent this behavior?
I'd like my PC to go to sleep, when I tell it to - even though the Excel spreadsheet is still open.
Thank you very much :)
After your Close lines you could
Set rs = Nothing
Set con = Nothing
although I cannot guarantee that this is what is causing your computer to not hibernate - more information would be required.
Is it possible to make query like SELECT from VBA in Excel, so I can query a PostgreSQL DB from Excel?
If is possible please explain me how to connect to the database. I was looking in Google but found no results.
Create a table or view in PostgreSQL that describes the data you want.
Use an ODBC or ADO connection from VBA to connect to PostgreSQL. If using ODBC you'll need to create a DSN via odbcad32.exe then use the DSN in VB, it isn't easy to just connect directly.
See:
Using ADO in VBA to connect to PostgreSQL
PostgreSQL Query to Excel Sheet
http://jackdebear.blogspot.com.au/2011/11/connecting-to-postgres-from-excel.html
Enabling import/export flows between a remote postgres database and excel workbooks
Does ADO work with ODBC drivers or only OLE DB providers?
How to put query results into a datatable with Excel VBA and ADO?
Better written eample that uses Oracle, but the principles are the same - ODBC/ADO.
Here's some code can use as reference. Hope it helps.
Sub SelectBasic()
Dim objDb_con
Dim strSomeValue As String
Set objDb_con = CreateObject("ADODB.Connection")
Set Rsdatatype = CreateObject("ADODB.RecordSet")
glbConnString = Trim(ActiveSheet.Range("B1").Value)
//Connection string format:Driver={PostgreSQL Unicode};Database=MyDB;server=192.16*.*.**;UID=USERID;Pwd=pasword //comment it
If glbConnString = "" Then
MsgBox "Enter the Connection String"
Else:
objDb_con.Open glbConnString
strSql = "select strSomeValue from SOMETABLE where Something=1"
Rsdatatype.Open strSql, objDb_con, adOpenKeyset, adLockpessimistic
If Rsdatatype.EOF = False Then strSomeValue = Rsdatatype.Fields(0).Value
Rsdatatype.Close
End If
objDb_con.Close
End Sub
Even for 64-bit Windows, Excel VBA needs the 32-bit ODBC driver.
Create a DSN via %windir%\SysWOW64\odbcad32.exe. Indeed, typing odbcad32.exe points towards the 64-bit version where you can't find the proper 32-bit drivers by default.
Source: https://github.com/windweller/postgresql-excel-addIn