在VBA中,如何使用ADODB同时查询硬盘上的同一文件?

发布于 2024-09-26 02:34:35 字数 826 浏览 4 评论 0原文

我有一些如下所示的 VBA 代码,目的是查询 csv 文件并带回一些记录。但是,我希望能够从两台计算机同时查询同一文件(位于网络驱动器上)。我尝试使用只读模式,但它仍然不起作用。请帮忙?

   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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-10-03 02:34:35

制作 csv 的本地临时副本并从中导入可能会更容易。

如果由于 .csv 的大小而这不切实际,您可以检查该文件是否被其他用户锁定,并处于循环状态,直到该文件可用。

It may be easier to just make a local temporary copy of the csv and import from that.

If that's not practical due to the size of the .csv, you could check to see if the file is locked by another user and sit in a loop until it becomes available.

入怼 2024-10-03 02:34:35

这是 CSV 无法完成的。

It can't be done with a CSV.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文