简体   繁体   中英

Excel VBA: Connection Refresh error

I have an access database sitting in a shared folder on a network drive.

It's accessed by multiple computers through excel. The users have permission to update and modify the database through the excel sheet. The database is available as an embedded table in the excel sheet.

I'm working on Excel 2007.

In order for this to work, I have a macro run automatically which changes the Data Source path to that of the computer. For example, on my PC, it could be located at C:/accessDB.accdb, but on user X's PC the data could be located at Z:/accessDB.accdb.

The update which is run is as follows:

Dim connString As String
Dim folderLoc As String
Dim cn As WorkbookConnection
Dim oledbCn As OLEDBConnection

Set cn = ThisWorkbook.Connections(local_table)
Set oledbCn = cn.OLEDBConnection

folderLoc = ThisWorkbook _
            .sheets(settingSheet) _
            .Range(settingDbFileFolder).Value _
            & "\" & _
        ThisWorkbook _
            .sheets(settingSheet) _
            .Range(settingDbFileName).Value

connString = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & _
            folderLoc & _
            ";Mode=Share Deny None;Extended Properties=" & Chr(34) & Chr(34) & ";Jet OLEDB:System database=" & Chr(34) & Chr(34) & _
            ";Jet OLEDB:Registry Path=" & Chr(34) & Chr(34) & ";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1" & _
            ";Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=" & Chr(34) & Chr(34) & _
            ";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False" & _
            ";Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False"

If oledbCn.Connection <> connString Then
    oledbCn.Connection = connString
End If

When that is done, I loop through the database connections, and refresh the database connections. The code is as follows:

    Dim conn As OLEDBConnection
    If ThisWorkbook.Connections.Item(I).name = databaseName Then
        Set conn = ThisWorkbook.Connections.Item(I).OLEDBConnection
        conn.SourceDataFile = ""
        conn.Refresh
        conn.MaintainConnection = False
    End If

This have been working fine for months. However after my colleagues have restarted their computers, at the point conn.Refresh I get an error on their PCs but not my own as follows:

Run-time error '-2147417848 (80010108'

Method 'Refresh of object  'OLEDBConnection' failed

The connection object looks like this:

The Connection Properties looks like this:

Any help or guidance would be appreciated, it's a very frustrating bug as it's been working for months without any error - not to mention, it's working fine for my PC.

This seems to be that it isn't to do with the code and everything to do with windows user permissions for the shared file on the drive I'm accessing it from (thanks to NinjaLlama for pointing me in the right direction). Putting this into a standard database is obviously the way to go, however IT constraints are preventing this from happening for the moment.

What I did to get it to work was open a blank instance of excel on the user's PC. I then went to data -> "From Access" and clicked on the.accdb file. I then clicked through all the various steps and it imported the Access table into the sheet. I then closed this sheet, but not the instance . Then, I opened the file in the instance, ran the macro and lo and behold it has refreshed without any trouble.

Connecting this way has obviously unlocked some permissioning somewhere in the file system / on the file. Although this isn't really a solution, it's at least a significant step in figuring out where the error is comming from.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM