在VBA中,如何使用ADODB同时查询硬盘上的同一文件?
我有一些如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
制作 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.
这是 CSV 无法完成的。
It can't be done with a CSV.