Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:54:44+00:00 2026-05-29T08:54:44+00:00

I am having trouble finding clear and reliable examples of connecting to a PostgreSQL

  • 0

I am having trouble finding clear and reliable examples of connecting to a PostgreSQL database from Excel using VBA ADO. Admittedly, I am new to VBA and most examples and tutorials are very Access or MSSQL centered. (I work mostly in Ruby, Rails, Perl and PostgreSQL.)

I am looking for code to connect and return a simple query (SELECT * FROM customers;) to an Excel sheet. Connection parameters (server ip, user, pass, database) are located within cells in a separate worksheet.

I appreciate your help and patience.

Code:

Sub ConnectDatabaseTest()
Dim cnn As ADODB.connection
Dim cmd As ADODB.Command
Dim param As ADODB.Parameter
Dim xlSheet As Worksheet
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim i As Integer

' Connection Parameters
Dim strUsername As String
Dim strPassword As String
Dim strServerAddress As String
Dim strDatabase As String
' User:
strUsername = Sheets("CONFIG").Range("B4").Value
' Password:
strPassword = Sheets("CONFIG").Range("B5").Value
' Server Address:
strServerAddress = Sheets("CONFIG").Range("B6").Value
' Database
strDatabase = Sheets("CONFIG").Range("B3").Value

Set xlSheet = Sheets("TEST")
xlSheet.Activate
Range("A3").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select

Set cnn = New ADODB.connection
sConnString = "DRIVER={PostgreSQL Unicode};DATABASE=" & strDatabase & ";SERVER=" & strServerAddress & _
    ";UID=" & strUsername & ";PWD=" & strPassword
cnn.Open sConnString

cmd.ActiveConnection = cnn

Dim strSQL As String
strSQL = "SELECT * FROM customers"

cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
cmd.ActiveConnection = cnn
cmd.CommandText = strSQL
...

It seems to break here: cmd.ActiveConnection = cnn

EDIT: added sample code.

EDIT: sConnString gets set to:

DRIVER={PostgreSQL35W};DATABASE=my_database;SERVER=1.2.3.4;UID=analyst;PWD=sekrit

UPDATE 2/7: I changed the ‘DRIVER’ parameter in the connection string:

    sConnString = "DRIVER={PostgreSQL Unicode};DATABASE=" & strDatabase & ";SERVER=" & strServerAddress & _
    ";UID=" & strUsername & ";PWD=" & strPassword & ";"

…and I get a different error: ‘Run-time error 91: Object variable or With block variable not set’

Hm. Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:54:45+00:00Added an answer on May 29, 2026 at 8:54 am

    I wan’t using a DSN as I am using an ODBC driver as opposed to OLE DB. By referencing a DSN, the above code works with very few changes.

    See this question for how I found the answer once I began to suspect OLE DB/ODBC to the issue.
    Does ADO work with ODBC drivers or only OLE DB providers?

    New Code here:

    Sub GetCustomers()
    Dim oConn As New ADODB.connection
    Dim cmd As New ADODB.Command
    ' Connection Parameters
    Dim strUsername As String
    Dim strPassword As String
    Dim strServerAddress As String
    Dim strDatabase As String
    ' User:
    strUsername = Sheets("CONFIG").Range("B4").Value
    ' Password:
    strPassword = Sheets("CONFIG").Range("B5").Value
    ' Server Address:
    strServerAddress = Sheets("CONFIG").Range("B6").Value
    ' Database
    strDatabase = Sheets("CONFIG").Range("B3").Value
    
    
    oConn.Open "DSN=my_system_dsn;" & _
        "Database=" & strDatabase & ";" & _
        "Uid=" & strUsername & ";" & _
        "Pwd=" & strPassword
    
    Set xlSheet = Sheets("CUSTOMERS")
    xlSheet.Activate
    Range("A3").Activate
    Selection.CurrentRegion.Select
    Selection.ClearContents
    Range("A1").Select
    
    Dim strSQL As String
    strSQL = "SELECT * FROM customers"
    
    cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
    cmd.ActiveConnection = oConn
    cmd.CommandText = strSQL
    
    Set rs = New ADODB.Recordset
    Set rs = cmd.Execute
    
    For i = 1 To rs.Fields.Count
        ActiveSheet.Cells(3, i).Value = rs.Fields(i - 1).Name
    Next i
    
    xlSheet.Range(xlSheet.Cells(3, 1), _
        xlSheet.Cells(3, rs.Fields.Count)).Font.Bold = True
    
    ActiveSheet.Range("A4").CopyFromRecordset rs
    
    xlSheet.Select
    Range("A3").Select
    Selection.CurrentRegion.Select
    Selection.Columns.AutoFit
    Range("A1").Select
    
    rs.Close
    oConn.Close
    
    Set cmd = Nothing
    Set param = Nothing
    Set rs = Nothing
    Set cnn = Nothing
    Set xlSheet = Nothing
    End Sub
    

    The System DSN is configured to use the PostgreSQL Unicode driver. I chose not to use OLE DB even though there is a provider available. If you look at PGFoundry, you will see it has many problems and has not been updated in several years.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble finding/getting any logging output from a web service. I have a
I'm using the python optparse module in my program, and I'm having trouble finding
I'm having trouble finding a clear explanation of connection pooling. I'm building an app
I'm having trouble finding a clear answer to this question so I thought I'd
I am having trouble finding a surefire to copy only visible cells from one
I'm having trouble finding out the proper syntax for selecting from a table all
I am having trouble finding a clear, sensible example of usage of context with
I'm having trouble finding a Yacc or Bison that doesn't require using darwinports for
Ok Im having trouble finding out how to select a full database for backup
I am having trouble finding clear documentation on how to set up a batch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.