I have an Excel file that executes SQL queries on an MS Access database with local tables, works fine. Now I have to get this database to work on a network, we just have SharePoint. I discovered the SharePoint lists, so I exported my tables to these lists and now I have an Access database with linked tables.
Problem is that I can't manage to use my queries from Excel on this database like I use to do. Is it possible? Because I can do these queries from Access and it works, so I don't understand why it wouldn't from Excel. Here is my function:
Sub test()
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & "C:\...\BADO.accdb"
strSql = "SELECT Count(*) FROM Programmes;"
cn.Open strConnection
Set rs = cn.Execute(strSql)
MsgBox rs.Fields(0) & " tuples dans Programmes"
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
The part of the connection cn.Open
works, but then when it tries to execute cn.Execute
, Excel goes into an endless loading and crashes. This code works only with local tables.
If it's not possible, I was thinking that I could let the database with its local tables on SharePoint and let users sync it with OneDrive, but I don't know how the sync works. If the database is heavy, I don't want them to upload every time the full size. So, is there a way to make this code to work?
Ultimately, this is a networking environment issue. Testing your code from Excel where both the workbook and Access database resides on a network that connects to SharePoint via a Local Area Network (LAN), I experienced no issue of hanging or crashing.
However, running both workbook and Access database from a local CPU that connects to SharePoint via wireless or remote connection, I did experience severe hanging and crashing.
Therefore, consider following options in order of complexity:
LAN Connection : Situate Excel workbook and Access database on a network space that makes a LAN connection to SharePoint. This may involve users remotely connecting via VPN to a computer on premise.
Action Queries : Have MS Access run make-table queries from linked to local tables of your simple count aggregations:
SELECT Count(*) AS [Count] INTO Programmes_Count FROM Programmes;
Alternatively, using append queries on a table created in advance which may be better than DROP TABLE
and re-create each time:
DELETE FROM Programmes_Count; INSERT INTO Programmes_Count ([Count]) SELECT Count(*) FROM Programmes;
Such action queries can be saved and then automated with Access macros or modules called from Excel. Then have Excel access those local tables.
COM Automation : Have Excel launch Access.Application as a COM object and then have the database itself query the linked table.
Sub RunQuery() Dim accApp, db, qdef, rst As Object Dim strSQL As String Set accApp = CreateObject("Access.Application") ' LAUNCH ACCESS IN BACKGROUND accApp.OpenCurrentDatabase "C:\Path\To\Database.accdb" strSQL = "SELECT Count(*) AS [Count] FROM Programmes;" Set db = accApp.CurrentDb() ' ASSIGN UNDERLYING DB Set qdef = db.CreateQueryDef("", strSQL) ' CREATE QDEF OR USE SAVED Set rst = qdef.OpenRecordset() ' OPEN RECORDSET ' LAUNCH MESSAGE BOX MsgBox rst.Fields(0) & " tuples dans Programmes" ' CLOSE OBJECTS rst.Close: qdef.Close accApp.DoCmd.CloseDatabase accApp.Quit ' RELEASE RESOURCES Set rst = Nothing: Set qdef = Nothing: Set db = Nothing: Set accApp = Nothing End Sub
Even better bypass Access application and query Access via DAO:
Sub RunQuery() Dim conn, db, qdef, rst As Object Dim strSQL As String Set conn = CreateObject("DAO.DBEngine.120") ' LAUNCH DAO CONNECTION Set db = conn.OpenDatabase("C:\Path\To\Database.accdb") strSQL = "SELECT Count(*) AS [Count] FROM Programmes;" Set qdef = db.CreateQueryDef("", strSQL) ' CREATE QDEF OR USE SAVED Set rst = qdef.OpenRecordset() ' OPEN RECORDSET ' LAUNCH MESSAGE BOX MsgBox rst.Fields(0) & " tuples dans Programmes" ' CLOSE OBJECTS rst.Close: db.Close ' RELEASE RESOURCES Set rst = Nothing: Set qdef = Nothing Set db = Nothing: Set conn = Nothing End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.