将 DAO 转换为 ADO
我正在使用一个 Access 2003 数据库,该数据库有一个使用 DAO 代码的子例程。此代码循环遍历表定义并刷新 ODBC 连接字符串。我想将其转换为 ADO,这样我就不必引用 DAO 对象库。这是代码...
Public Sub RefreshODBCLinks(newConnectionString As String)
Dim db As DAO.Database
Dim tb As DAO.TableDef
Set db = CurrentDb
For Each tb In db.TableDefs
If Left(tb.Connect, 4) = "ODBC" Then
tb.Connect = newConnectionString
tb.RefreshLink
Debug.Print "Refreshed ODBC table " & tb.Name
End If
Next tb
Set db = Nothing
MsgBox "New connection string is " & newConnectionString, vbOKOnly, "ODBC Links refreshed"
End Sub
我不确定的部分是如何循环遍历表并获取/设置它们的连接字符串。
I am working with an Access 2003 database that has a subroutine using DAO code. This code loops through the table definitions and refreshes the ODBC connection string. I would like to convert this to ADO so I do not have to reference the DAO object library. Here is the code ...
Public Sub RefreshODBCLinks(newConnectionString As String)
Dim db As DAO.Database
Dim tb As DAO.TableDef
Set db = CurrentDb
For Each tb In db.TableDefs
If Left(tb.Connect, 4) = "ODBC" Then
tb.Connect = newConnectionString
tb.RefreshLink
Debug.Print "Refreshed ODBC table " & tb.Name
End If
Next tb
Set db = Nothing
MsgBox "New connection string is " & newConnectionString, vbOKOnly, "ODBC Links refreshed"
End Sub
The part I am unsure of is how to loop through the tables and get/set their connection strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DAO 确实是最好的选择,您无法使用 ADO 刷新链接,而是需要使用 ADOX (一些相关代码 这里)。
您可以通过
Jet OLEDB:Link Provider String
访问连接字符串DAO is really best for that, you cannot refresh the link with ADO rather you would need to use ADOX (Some relevant code here).
You can acces the connection string via
Jet OLEDB:Link Provider String
如果您的目标是避免引用 DAO,则只需修改现有过程以使用 DAO 的后期绑定即可。例如,该子程序应该在没有 DAO 参考集的情况下工作。
在编写代码时,您不会有 Intellisense 来帮助您处理 DAO 属性、方法和常量,但代码仍然可以使用后期绑定。
我认为如果您决定避免 DAO 引用,这将是您最简单的选择。然而,我从来没有开发过没有DAO引用的Access项目,我不明白你为什么反对添加它。
编辑:此外,如果您使用后期绑定和任何 DAO 常量,您的代码必须使用常量值而不是名称。
If avoiding a reference for DAO is your goal, you could just modify your existing procedure to use late binding for DAO. As an example, this sub should work without a reference set for DAO.
You would not have Intellisense to help you with DAO properties, methods, and constants while writing the code, but the code can still work with late binding.
I think this would be your easiest alternative if you are determined to avoid a DAO reference. However, I have never developed an Access project without a DAO reference, and I don't understand why you are opposed to adding it.
Edit: Also if you use late binding and any DAO constants, your code must use the constant value rather than the name.