(VBA) Building a ADODB.Connection to Access from Excel - vba

I'm been searching for a while and I can't find any help on this issue.
I have a shared Access 2007 database as a source. I have excel pivots that are linked to a table in that database. I will utimately have multiple excel files linked to the database (and multiple users), and for that reason I'd like to avoid residual open connection to the DB for Access performance reasons, even if the connections are read only.
I've built the connection in excel, but I am attempting to so is write VBA code to open the connection to the access table refresh the pivot cache, then drop the connection. I know that I'm not putting the pieces together correctly. Can someone help me out? Thank you in advance.
Sub ConnectToAccessAttempt()
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DB_Name.accdb;Persist Security Info=False"
ActiveWorkbook.PivotCaches(1).CommandText = "table_Name"
ActiveWorkbook.RefreshAll
End Sub

Related

Connect Microsoft Access to phppgadmin to query in PostgreSQL

My goal is to import data into Microsoft Access to create a database which I can reference from an excel dashboard for analysis.
I can't find any information on how to connect access to allow me to query the database on phpPgAdmin.
Any advice, direction or solution is highly appreciated.
Please let me know if there more details are necessary.
MS Access is a multifaceted thing as many tend to conflate and confuse its frontend GUI .exe application and the distinct backend database (JET/ACE SQL engine which are Windows .dll files). Most of the time we refer to its MS Office app. Technically, MS Access is really the same type of product as phppgadmin: a GUI console to a database, only its default database is the aforementioned engine but can also integrate other ODBC/OLEDB-connected backends including Postgres, Oracle, MySQL, SQL Server, etc.
Through various means, you can integrate MS Access as a medium between PostgreSQL and Excel without any single migration (export/import) of data.
Linked Tables - Directly connect to Postgres tables using its ODBC Driver.
Pass-through queries - Create saved queries using Postgres dialect within MS Access.
ADO Connections (see Importing data programmatically and by using functions) - Bypass MS Access and have Excel connect directly to Postgres also using OLEDB provider or ODBC driver. Below is the programmatic version showing two connection string examples, but you can save connection objects via the Excel ribbon UI.
Dim strConnection
' REFERENCE Microsoft ActiveX Data Objects, #.# Library
Dim conn As ADODB.Connection, rst As ADODB.Recordset
' ODBC AND OLEDB CONNECTIONS (SELECT ONE)
strConnection = "Driver={PostgreSQL};Server=IPaddress;Port=5432;" _
& "Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
strConnection = "Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;" _
& "location=myDataBase;User ID=myUsername;password=myPassword;"
conn.Open strConnection
rst.Open "SELECT * FROM myPGTable", conn
By the way, above is the VBA version to be run in an Excel macro but ADO is a COM object and hence can be integrated in COM-interfaced languages including PHP, Python, R, Java, etc.

Get data from ODBC in MS Project

I have a typical project that uses "% work complete". This value actually is entered in an ERP which could be accessed by an ODBC connection. However, I can't figure out how to open this connection. Using VBA I get a compilation error in this line
Dim con As New ADODB.Connection
Is it even possible to gather data from an ODBC source in MS Project?

Is it possible to have a SQL database with Excel files as "data source"?

Is it possible to make some kind of link from SQL to Excel making the Excel documents the true datasource? I don't want any data to be stored in SQL, all data editing will be done in the Excel sheets.
I know this is far from optimal but I don't have a choice, the data needs to stay in Excel.
I know there is an option to do this kind of links in Access and there I can access the data from within VS but I would really prefer SQL.
It's also possible to use the Jet driver directly from your application and skip out SQL server.
I know this isn't a proper answer to your question but another thought that may be useful :)
Yes, you can set up excel as a linked server. You'll need to use a jet driver but this is only available on 32bit machines (Microsoft dropped their support a while back). In other words, this is completely not scale-able and not recommended in most production environments.
If you want to choose Excel as a datasource then you can connect to it using .Net Oledb provider.
Consider VB.Net example below to read rows on excel sheet:
imports System.Data.OleDb
dim connstr as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\exceldb.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim conn As New OleDbConnection(connstr)
conn.Open()
Dim da As New OleDbDataAdapter("select * from [Sheet1$]", conn)
Dim dt As New DataTable()
da.fill(dt)
For i As Integer = 0 To dt.Rows.Count - 1
'do your thing
next
conn.close()
Regards

Query Timeout Settings in Excel

new to the site I'd like to give it a shot and see if I can get any answers regarding SQL Querys in Excel.
Is there a way to handle if a Timeout occurs in Microsoft Excel (mostly 2007/2010 on Win XP/Vista/7)? As you know you can connect Excel to a Microsoft SQL Server and run your query via Excel. The only thing is that I don't seem to find any Timeout options for this. And for an example, if there is a bad query, this might lock other tables in the SQL Server (2005) database.
I'm not looking for a script. It's more like settings I need and if possible I would like to add these Timeout settings to a specific Windows user account. Settings in either SQL Server 2005 or in Microsoft Excel 2007/2010.
Best regards
/Henrik
Use the CommandTimeout property
Dim objCommand As ADODB.Command
Set objCommand = New ADODB.Command
objCommand.CommandTimeout = 99 '
objCommand.ActiveConnection = cnConn
objCommand.CommandText = "DELETE Users WHERE IdLevel < 98"
objCommand.Execute

access to SQL copy

We have an Access DB which has a set of local tables and input forms etc. in which a user maintains their data.
We also have a SQL DB with the same tables which is used to displays the data in a web search form.
What is the best way to allow the user to udate his changes to the SQL db while keeping the working copy local so he can work offline and then push the files when he is happy with new version of the data?
My first thought was add the SQL tables as linked tables I could then truncate (access does like that) or delete the content in each table and then do an insert for each table.
Can I call a SP from access on the SQL to truncate the tables as I am have problem running deletes
I really do want to get it down to the user running a macro/sql call that is repeatable etc.
Thanks for your help
You should be able to use the ADODB.Command object to execute stored procedures.
EDIT:
This example is copied from Using ADO in VB and Access
Sub ADO_COMMAND_CONNECTION_TEST()
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim strConn As String
cmd.ActiveConnection = " DRIVER={SQL Server};" & _
"Server=UKDUDE;DATABASE=pubs;UID=sa;PWD=;"
cmd.CommandText = "byroyalty"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1).Value = 25
Set rs = cmd.Execute
' Recordset now has authors with 25% royalty.....
End Sub
Don't ever use MS Access linked tables with MS SQL.
Not only are they slow, but Access can leave open client-side write cursors on the tables referenced. That's a really dumb way to create lots of deadlocks, but Access does it anyway.
Microsoft significantly improved this when they added Access Data Projects - in these the entire back end is replaced with SQL and Access just supplies the forms.
If you want user actions to write directly back then ADPs are by far the best method.
If you want to cache changes locally in your Access DB and then send them up to SQL you have a far more complex problem. You will need to be far more specific on exactly how you want synchronisation to happen - for instance if two users make offline changes who wins when they connect?
I don't understand why you just don't link directly to the SQL Server data and use it directly, rather than going to the trouble of maintaining a second copy of it. This is the standard Access way to do things -- why are you resisting the natural capabilities of the tool you're using?