Re: VBA to connect to postgresql from MS Access - Mailing list pgsql-general

From Bret Stern
Subject Re: VBA to connect to postgresql from MS Access
Date
Msg-id 1527872841.3939.24.camel@bret.machinemanagement.com
Whole thread Raw
In response to Re: VBA to connect to postgresql from MS Access  ("Arnaud L." <arnaud.listes@codata.eu>)
List pgsql-general
Not sure what version of access you're using or how you are calling the
connection..ADO, DAO, .NET

I don't hard code connection strings anymore, but I did when I first started.
Nowadays I call a registry function to put the drivers on a specific pc into a listbox
and have a setup module which then builds the connection string on the fly from the
select listbox value.

The below sample may not work, I haven't tested it, but the basic elements are there to help
you start thinking about how it does work.

The DRIVER= element of the connection must have the name of the driver installed on your
pc. Usually they can be found under the ODBC administrator tool, found in Control Panel, Administrative Tools,
ODBC Data Source Administrator. If you're creating a DSN-LESS connection, then click the Drivers tab and scroll to the
Postgresql drivers

The DRIVER= element must exactly match the name of the driver in the [Drivers] tab under the ODBC administrator
tool. In the code example below, the driver name on my pc is PostgreSQL ANSI

The other elements are well documented
Server= (an ip address or hostname of the database server you are connecting to)
Port= (Can generally be left at 5432...the postgresql default port)
Database= (is the case sensitive name of the postgresql database you are connecting)
UID= (postgresql user name to make the connection under)
PWD= (the password the connecting user)

If you are still having problems, there is the pg_hba.conf file which may block access.
You can read about that here. https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html


try creating a simple form, add a button, and have the button click event call the below
function AFTER you have modified the elements to suit your database name, server etc

hopefully some of this info will help make sense


Function pg_db_open() as boolean
   Dim conNew As New ADODB.Connection
   Dim cs as string
   cs = "DRIVER=PostgreSQL ANSI; Server=your-server-ip; Port=5432; Database=your-database-name; UID=username; PWD=password"

    On Error GoTo conError

    conNew.ConnectionString = cs
    conNew.Open
    Set conNew = Nothing

    pg_db_open = True
    Exit Function

conError:
    Set conNew = Nothing
    pg_db_open = False
End Function




On Fri, 2018-06-01 at 14:27 +0200, Arnaud L. wrote:
Le 01-06-18 à 14:21, Łukasz Jarych a écrit :
> ODBC;DSN=PostgreSQL35W;DATABASE=AccessTest;SERVER=localhost;PORT=5432;*CA=d;A7=100;B0=255;B1=8190;BI=0;C2=;CX=1c305008b;A1=7.4*
> 
> When i was searching code for this in internet i found only:
>     DRIVER={PostgreSQL Unicode(x64)};DATABASE=AccessTest;SERVER=localhost;PORT=5432;UID=postgres;PWD=1234;"
> 
> but this is not working, why? I do not know what these CA, BO, BI 
> strange paramaters.
Why is this not working, we cannot answer without the error message you 
get. You probably use the wrong driver name.

As for the CA, BO, etc... they are abreviations of the keywords. You can 
use either the keywords or the abreviations in your connection string, 
and they are all explained here :
https://odbc.postgresql.org/docs/config-opt.html



--
Bret Stern
Machine Management
Industrial and Commercial IT Services

707-775-9792 (cell-text-direct)

pgsql-general by date:

Previous
From: John Woltman
Date:
Subject: RPM Packaging Question - Fedora 28 & Postgis
Next
From: Adrian Klaver
Date:
Subject: Re: RPM Packaging Question - Fedora 28 & Postgis