是否有人知道在MS-Access 2010中自动链接和刷新Postgres链接表(通过ODBC)的VBA过程?这是因为我正在寻找一种无DSN连接,以方便用户使用。
答案 0 :(得分: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