File in Use error when using ADO to query closed workbook

bfreescott

Board Regular
Joined
Aug 6, 2015
Messages
115
I'm using an ado connection to query data from excel files on a shared network drive and I'm getting the standard "File in Use" message whenever another user has the file already open. Is that to be expected? My purpose in using ADO was two-fold: speed and being able to query a closed workbook. I suppose I didn't realize I would have the same issue I get when using Workbook.open. Is this a bug? Is there something wrong with my function? Workaround?

Code:
Function Get_Initial_VPK_Advance(wbName As String) As Variant
'
    On Error GoTo ErrorHandler

    Dim Conn As ADODB.Connection
    Dim ConnString As String
    Dim rs As New ADODB.Recordset

    ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & wbName & ";Extended Properties=Excel 12.0;"
    Set Conn = New ADODB.Connection
        Conn.Open ConnString

    If Not (Conn.State And 1) = 1 Then
        MsgBox "Source data cannot be found. Please check the location of the file you are querying."
        Set Conn = Nothing
        Set rs = Nothing
        Application.ScreenUpdating = True
        Application.StatusBar = False
        Exit Function
    End If

    rs.Open "SELECT * FROM InitialAdvance", Conn, adOpenStatic, adLockReadOnly, adCmdText 
    If Not rs.Fields(0).Value = Null Then
        Get_Initial_VPK_Advance = rs.Fields(0).Value
    End If

    rs.Close
    Conn.Close
    Set Conn = Nothing
    Set rs = Nothing

Exit Function
ErrorHandler:
    MsgBox "Error: " & Err.Number & " , " & Err.Description & Chr(13) & _
    "Procedure is: Get_Initial_VPK_Advance" & Chr(13) & ""
    rs.Close
    Conn.Close
    Set Conn = Nothing
    Set rs = Nothing
    Application.ScreenUpdating = True
    Application.StatusBar = False
'
End Function
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top