【问题标题】:Automatically link/refresh ODBC linked tables in MS Access via VBA通过 VBA 在 MS Access 中自动链接/刷新 ODBC 链接表
【发布时间】:2013-03-15 19:45:31
【问题描述】:

有人知道在 MS-Access 2010 中自动链接和刷新 Postgres 链接表(通过 ODBC)的 VBA 程序吗?这是因为我正在寻找一种无 DSN 的连接,以使用户更轻松。

【问题讨论】:

    标签: postgresql vba ms-access


    【解决方案1】:

    以下 VBA 代码将创建具有无 DSN 连接的 PostgreSQL 链接表...

    Sub linkTo_PostgreSQL()
        createLinkedTable_PostgreSQL "public.table1"
        ' repeat as necessary...
    End Sub
    
    Sub createLinkedTable_PostgreSQL(PostgreSQL_tableName As String)
        Dim cdb As DAO.Database, tbd As DAO.TableDef
        Set cdb = CurrentDb
        Set tbd = New DAO.TableDef
    
        tbd.Connect = "ODBC;Driver={PostgreSQL ODBC Driver(UNICODE)};Server=localhost;Port=5432;Database=linkedDB;Uid=pgUser1;Pwd=pgUser1password;"
        tbd.SourceTableName = PostgreSQL_tableName
        tbd.Name = Replace(PostgreSQL_tableName, ".", "_", 1, -1, vbTextCompare)  ' e.g. "public.table1"->"public_table1"
        tbd.Attributes = dbAttachSavePWD
    
        cdb.TableDefs.Append tbd
        Set tbd = Nothing
        Set cdb = Nothing
    End Sub
    

    以下代码将刷新任何现有 PostgreSQL 链接表的链接:

    Sub refreshLinked_PostgreSQL()
        Dim cdb As DAO.Database, tbd As DAO.TableDef
        Set cdb = CurrentDb
        For Each tbd In cdb.TableDefs
            If tbd.Connect Like "ODBC;Driver={PostgreSQL*" Then
                Debug.Print "Refreshing [" & tbd.Name & "] ..."
                tbd.RefreshLink
            End If
        Next
        Debug.Print "Done."
        Set tbd = Nothing
        Set cdb = Nothing
    End Sub
    

    【讨论】:

    • 非常感谢 Thopson 先生 它有效。还有一个问题,如果表已经链接并刷新它们,是否有一种简单的方法
    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多