Search code examples
vbacsvoledbadodb

In VBA, how do I use ADODB to query the same file on a hard drive at the same time?


I have some VBA code that looks like this and the aim is to query a csv file and bring back some records. However, I want to be able to query the same file (which sits on a network drive) at the same time from two computers. I tried using the readOnly mode but it still doesn't work. Please help?

   Dim cnt_string  As String
   cnt_string = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & "V:\Data\;" & _
        "Extended Properties = Text;"

    strSQL = "SELECT * FROM " & strData & ".csv " & strData & " WHERE (" & strData & ".APPLICATION_ASSIGNED_TO='" & strBrokerNumber & "')"
    Sheets("Broker").Activate

   Dim rs As ADODB.Recordset

   Set rs = New ADODB.Recordset
   Call rs.Open(strSQL, cnt_string, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
   Dim sh As Worksheet
   Set sh = Sheets("Broker")

   Call sh.Range("A10").CopyFromRecordset(rs)
   rs.Close
   Set rs = Nothing

Solution

  • It can't be done with a CSV.