VBA Excel connection Database Postgres

Source: Internet
Author: User
Tags dsn



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.