0

How can I query PostreSQL in VBA? I have tried the following which I found online but doesn't seem to work.

Sub query()

'Create Connection
Set conn = CreateObject("ADODB.Connection")
strCnx = "Driver={PostgreSQL UNICODE};Server=localhost;Database=databasename;uid=username;pwd=password123;"
conn.Open strCnx

'Query the Database
Set rs = CreateObject("ADODB.recordset")
rs.Open "select * from dm.acct_dim limit 10", conn

Record = rs.GetRows()
rs.Close
conn.Close

'Write results to file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\dbData.txt", 8, True)

For Each Item In Record
  objFile.WriteLine (Item)
Next

objFile.Close

End Sub

When I try this i get an error saying "could not connect to the server; No Connection could be made because the target machine actively refused it."

When I connect with pgadmin i use host, port, database, username and password. So I'm guessing i need those same things in the connection string but not exactly sure how that should be setup. I'm a rookie here. thanks for any help.

1

1 Answer 1

0

Looks like it was correct as it was. My problem was that I had wrong server. My bad. Thanks for trying to help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.