使用传递查询时更改连接参数

发布于 2024-09-24 23:38:30 字数 266 浏览 6 评论 0原文

我有一个 Access 2003 应用程序,我想通过传递查询创建使用存储过程的报告。除了一个例外,一切都工作正常。当我指定用于传递查询的存储过程时,我必须选择 DSN 来提供数据库连接信息。我需要能够通过代码更改传递查询中使用的存储过程的连接信息。这样我就可以从应用程序内切换到开发、生产、测试​​环境。

目前,我的所有数据访问(添加、更新、编辑)都使用 ADO,并且我通过 VBA 代码构建连接字符串。我不确定如何通过代码更改传递查询的连接信息。有什么想法吗?谢谢。

I have an Access 2003 application that I would like to create reports for using stored procedures via pass-through queries. Everything works fine with one exception. When I specify the stored procedure to use for the pass-through query I have to choose a DSN to provide the database connection info. I need to be able to change the connection info for the stored procedure used in the pass-through query via code. This is so I can switch to development, production, testing environments from within the application.

Currently all of my data access (additions, updates, edits) uses ADO and I build the connection strings via VBA code. I am unsure how how to change the connection info of the pass-through queries via code. Any thoughts? Thank you.

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

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

发布评论

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

评论(1

羁绊已千年 2024-10-01 23:38:30

查看传递查询的 Connect 属性。您可以使用 VBA 更改连接。此过程在 prod 和 dev 数据库连接之间切换。

Public Sub SwitchPassThroughConnection(ByVal pQuery As String, ByVal pTarget As String)
    Dim db As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim strConnect As String
    Dim blnError As Boolean

    Set db = CurrentDb()

    Select Case pTarget
    Case "dev"
        strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=dev;SERVER=cmpq;" & _
            "PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
            "B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
    Case "prod"
        strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=prod;SERVER=cmpq;" & _
            "PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
            "B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
    Case Else
        blnError = True
        MsgBox "Unrecognized target."
    End Select

    If Not blnError Then
        Set qdef = db.QueryDefs(pQuery)
        qdef.Connect = strConnect
        qdef.Close
    End If
    Set qdef = Nothing
    Set db = Nothing
End Sub

Look at the Connect property of your pass-through query. You can change Connect with VBA. This procedure switches between prod and dev database connections.

Public Sub SwitchPassThroughConnection(ByVal pQuery As String, ByVal pTarget As String)
    Dim db As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim strConnect As String
    Dim blnError As Boolean

    Set db = CurrentDb()

    Select Case pTarget
    Case "dev"
        strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=dev;SERVER=cmpq;" & _
            "PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
            "B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
    Case "prod"
        strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=prod;SERVER=cmpq;" & _
            "PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
            "B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
    Case Else
        blnError = True
        MsgBox "Unrecognized target."
    End Select

    If Not blnError Then
        Set qdef = db.QueryDefs(pQuery)
        qdef.Connect = strConnect
        qdef.Close
    End If
    Set qdef = Nothing
    Set db = Nothing
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文