有人知道在 MS-Access 2010 中自动链接和刷新 Postgres 链接表(通过 ODBC)的 VBA 程序吗?这是因为我正在寻找一个没有 DSN 的连接,以使用户更轻松。
问问题
3319 次
1 回答
2
以下 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
于 2013-03-16T14:00:13.677 回答