简体   繁体   中英

How to connect VBA to postgreSQL and run query

I am trying to run a query from an Microsoft excel application and have not been able to connect successfully. I have PostgreSQL 9.3 on my local machine, and am running 64 bit windows 7. I have a sample database name dvdrental which is a demo database. I simply need to connect to the database, run a query, and view the output in my worksheet(or immediate window, either one resolves the connection issue). Here is what I have so far which is not working.

Option Explicit
Public objConnection As ADODB.Connection 
Public strConnection As String

Public Sub TestPostgresConnection()
Dim strConnection As String
strConnection = "Driver={PostgreSQL Unicode};Server=localhost;Port=5432;   Database=dvdrental;UID=sa;PWD=wrox;"
Set objConnection = New ADODB.Connection
Set objRecordSet = New ADODB.Recordset
objConnection.Open strConnection
With objRecordSet
    .ActiveConnection = objConnection
    .Open "SELECT * FROM actor"
End With
Do While Not objRecordSet.EOF
    Debug.Print objRecordSet.Fields(0).Value
    objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
Set objRecordSet = Nothing
Set objConnection = Nothing
End Sub

Here is a list of my references;

Visual Basic For Applications Microsoft Excel 14.0 Object Library OLE Automation Microsoft Office 14.0 Object Library Microsoft Forms 2.0 Object Library Microsoft Access 14.0 Object Library Microsoft ADO Ext. 6.0 for DOL and Security Microsoft ActiveX Data Objects 2.8 Library Microsoft Windows Common Confrols 6.0 (SP6)

When I execute this test method TestPostgresConnection, I get "[Miscrosoft][ODBC Driver Manager] Data source name not found and no default driver specified"

My setup of postgres has been standard and I have simply followed the directions on their website for creating a local RDBMS for testing.

Can anyone tell me why I am not able to connect and run a query? None of the solutions have worked so far. Thanks.

My answer is a general answer. Giving back to the community. So be nice. I had a similar question and I used the following to set it up.

Note: I suggest using DSN instead of the driver then you can use a named connection that has the password already, rather than having the password in your code.

These instructions were a huge help generally: http://www.dashbay.com/2011/03/working-with-postgres-on-windows-via-odbc/

The download link in the link above didn't work for me. I found the ODBC downloads here: https://www.postgresql.org/ftp/odbc/versions/msi/ I think I downloaded this one: psqlodbc_09_05_0400-x86.zip

I used Konstantin's answer to get %WINDIR%\\SysWOW64\\odbcad32.exe from this link: PostgresSQL ODBC Drivers on Windows 7 not showing up

I also downloaded MS Power Query here which I found helpful: https://www.microsoft.com/en-us/download/details.aspx?id=39379

I am happy to edit my answer if I should add clarification.

Below is the sub for the query and below that is a sub that demonstrates how you use it.

Sub CcQueryPg(sSql As String, Optional sOdbcName As String = "ConnectionNameHere")


'Declare a Connection object
Dim cnDB As New ADODB.Connection

'Declare a Recordset Object
Dim rsRecords As New ADODB.Recordset

'Open the ODBC Connection using this statement
cnDB.Open sOdbcName
rsRecords.Open sSql, cnDB

'Close everything and set the references to nothing
rsRecords.Close
Set rsRecords = Nothing
cnDB.Close
Set cnDB = Nothing
End Sub

Sub SendQuery()

Call CcQueryPg("COPY  sometablenamehere FROM    '/mnt/somepathhere/somefilename.csv' DELIMITER ',' CSV HEADER;")

End Sub

The above file reference is to a file on a Linux machine. Your path format may differ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM