The first step is to download the Postres driver on the Internet, after installation,: https://www.devart.com/odbc/postgresql/download.html
Second step to create an ODBC data source
Click "Start-" control Panel-"Management tools-" data source (ODBC)-"User dsn-" Add "
Write VBA code after the installation is configured.
Private Sub CommandButton1_ Click()
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dataBase As String
Dim userName As String
Dim password As String
Dim DBname As String
Dim openCommand As String
dataBase = "PostgreSQL35W"
userName = "postgres"
password = "test1234"
DBname = "testRPA"
openCommand = "DSN=" & dataBase & ";" & "UID=" & userName & ";" & "PWD=" & password & ";" & "Database=" & DBname
cnn.Open openCommand
Dim SQL As String
"Quantity"
SQL = "select id from test where ID = ‘1001‘"
rs.Open SQL, cnn
While Not rs.EOF
MsgBox rs!ID
rs.MoveNext
Wend
rs.Close
cnn.Close
Set rs = Nothing
Set cn = Nothing
"Cut it off
SQL = "delete from test where ID = ‘1001‘"
Call cnn.Execute (SQL)
"Amendment
SQL = "update test set NAME = ‘name123‘ where ID = ‘1002‘"
Call cnn.Execute (SQL)
End Sub
You can do it!
VBA Excel connection Database Postgres