open adodb connection to excel file in read only mode

倾然丶 夕夏残阳落幕 提交于 2019-12-08 11:56:15

问题


Case: opening as read-only an excel file (.xlsx) using adodb.connection inside a VBA script in Excel 2013 on Windows 7 64bit.

Problem: the excel file seems to be opened for editing even setting the Mode parameter as Read in the connection string of adodb in order to open the connection in read-only mode:

szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                "Data Source=" & SourceFile & ";" & _
                "Mode=Read;" & _
                "Extended Properties=""Excel 12.0;HDR=Yes;"";"

Test: I set a break point after opening the connection as below

Set rsCon = CreateObject("ADODB.Connection")
rsCon.Open szConnect

and to test if the source file is opened in 'Mode=Read;' through the adodb and it is still able to be opened in write/edit mode by other users/connections I tried to open it through file explorer while the script was in break mode but an alert saying the file "is locked for editing" is popped up and I am forced to open in read-only mode.

So what might be wrong?

Here I am pasting the whole code mentioned in the below comments so people might run the code to find the problem, it is still a problem and the reason for wanting to open the file in read-only mode is to both avoid corrupting the source file due any bugs in the code and also let other users open the file in write mode to edit the file:

Public Sub ADODBTEST()

    'the paths to a shared file in local network pcs
    Dim szSourceFile As String
    'you might want to comment/uncomment one of the following szSourceFile paths for testing purposes
    szSourceFile = "\\NetworkPC\READONLYACCESS\SourceFile.xlsx" 'A source file shared in a folder giving read-only access
    'szSourceFile = "\\NetworkPC\WRITEACCESS\SourceFile.xlsx" 'A source file shared in a folder giving write access

   'the connection string that sets the Mode=Read
    Dim szConnect As String
    szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                "Data Source=" & szSourceFile & ";" & _
                "Mode=""Read"";" & _
                "Extended Properties=""Excel 12.0;HDR=No;"";"

    'creating the rsCon connection object
    Dim rsCon As Object
    Set rsCon = CreateObject("ADODB.Connection")
    'opening a connection to the SourceFile.xlsx through szConnect connection string
'***THE LINE WHERE ALL THE PROBLEMS HAPPEN: might be a good idea to set a breakpoint here
    rsCon.Open szConnect

'THE REST OF THE CODE IS MOSTLY FOR OUTPUT DEMONSTRATION
    'SQL code needed to read data from SourceFile.xlsx
    Dim szSQL As String
    szSQL = "SELECT * FROM A1:A1;"

    'creating rsData object
    Dim rsData As Object
    Set rsData = CreateObject("ADODB.Recordset")

    'opening and reading data according to szSQL and rsCon
    rsData.Open szSQL, rsCon, 0, 1, 1

    'Outputing some data for more clarification
    Dim szData As String
    szData = rsData.Fields(0).Value

    'in case the source file pops-up in foreground and is activated
    'if VBA does not reference ThisWorkbook.Worksheets(1) the read data
    'unwantedly is copied to the source file that is activated
    ThisWorkbook.Worksheets(1).Cells(1, 1).Value = "Connection String (Mode=Read): " & rsCon.ConnectionString
    ThisWorkbook.Worksheets(1).Cells(2, 1).Value = szData

    'cleaning the connection and data
    rsData.Close
    Set rsData = Nothing
    rsCon.Close
    Set rsCon = Nothing

End Sub

回答1:


Not sure if I'm not saying the banal thing, but why to have user possibility to open the file while your code runs? I found this page cause was looking for a solution of a problem that file was blocked after the code runs. The solution was very simple - first close, then set to nothing.

rsCon.Close

set rsCon = Nothing



来源:https://stackoverflow.com/questions/31941487/open-adodb-connection-to-excel-file-in-read-only-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!