Does anybody knows a VBA procedure that automatically link and refresh Postgres linked tables (via ODBC) in MS-Access 2010? It's because I'm looking for a DSN-less connection to make things easier for the users.
1
votes
1 Answers
2
votes
The following VBA code will create the PostgreSQL linked tables with DSN-less connections...
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
The following code will refresh the links for any existing PostgreSQL linked tables:
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