将 DAO 转换为 ADO

发布于 2024-09-06 08:54:45 字数 683 浏览 8 评论 0原文

我正在使用一个 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 技术交流群。

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

发布评论

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

评论(2

北笙凉宸 2024-09-13 08:54:45

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

月野兔 2024-09-13 08:54:45

如果您的目标是避免引用 DAO,则只需修改现有过程以使用 DAO 的后期绑定即可。例如,该子程序应该在没有 DAO 参考集的情况下工作。

Public Sub DAO_without_reference()
    Dim db As Object
    Dim td As Object
    Set db = CurrentDb
    For Each td In db.TableDefs
        Debug.Print td.Name
    Next td
    Set db = Nothing
End Sub

在编写代码时,您不会有 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.

Public Sub DAO_without_reference()
    Dim db As Object
    Dim td As Object
    Set db = CurrentDb
    For Each td In db.TableDefs
        Debug.Print td.Name
    Next td
    Set db = Nothing
End Sub

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.

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