Jump to content
Sign in to follow this  

PostGres SQL ODBC connector

Recommended Posts

This is manual for VB


psqlODBC HOWTO - Visual Basic

Author: Dave Page (dpage@postgresql.org)

Release Date: 13 November 2001

Description: Example based Mini-Howto on Accessing PostgreSQL from Visual Basic

This document provides some sample code to get you started with Visual Basic & PostgreSQL.

Requirements to get the subroutines to work:

* Visual Basic 5/6

* A reference in the VB project to Microsoft ActiveX Data Objects

* A PostgreSQL datasource.

The example code shown below may need some modification to make it actually work in your environment. There is one table used in the example:


id int4,

data text,

accessed timestamp



Sub Main()

Dim cn as New ADODB.Connection

Dim rs as New ADODB.Recordset

'Open the connection

cn.Open "DSN=<MyDataSourceName>;" & _

"UID=<MyUsername>;" & _

"PWD=<MyPassword>;" & _


'Clear the table

cn.Execute "DELETE FROM vbtest;"

'For updateable recordsets we would typically open a Dynamic recordset.

'Forward Only recordsets are much faster but can only scroll forward and

'are read only. Snapshot recordsets are read only, but scroll in both


rs.Open "SELECT id, data, accessed FROM vbtest", cn, adOpenDynamic, adLockOptimistic

'Loop though the recordset and print the results

'We will also update the accessed column, but this time access it through

'the Fields collection. ISO-8601 formatted dates/times are the safest IMHO.

While Not rs.EOF

Debug.Print rs!id & ": " & rs!data

rs.Fields("accessed") = Format(Now, "yyyy-MM-dd hh:mm:ss")




'Add a new record to the recordset


rs!id = 76

rs!data = 'More random data'

rs!accessed = Format(Now, "yyyy-MM-dd hh:mm:ss")


'Insert a new record into the table

cn.Execute "INSERT INTO vbtest (id, data) VALUES (23, 'Some random data');"

'Refresh the recordset to get that last record...


'Get the record count



MsgBox rs.RecordCount & " Records are in the recordset!"


If rs.State <> adStateClosed Then rs.Close

Set rs = Nothing

If cn.State <> adStateClosed Then cn.Close

Set cn = Nothing

End Sub

Useful Functions

' The escapeString function can be used to fix strings for us in INSERT and

' UPDATE SQL statements. It will take a value, and return it ready for

' use in your statement as NULL, or quoted with backslashes and single quotes

' escaped.

Function escapeString(vValue As Variant) As String

If IsNull(vValue) Then

escapeString = "NULL"


escapeString = "'" & Replace(Replace(vValue, "", ""), "'", "''") & "'"

end if

End Function


My script is:


$sql = "INSERT INTO Incoming_rates (Destination, Area Codes, Price, Valid From) VALUES ('Albania Mobile', '0035538, 0035567, 0035568, 0035569', '0,1684', '26.11.07');"

Func postgres_query($query)
$oConn = ObjCreate("ADODB.Connection")
$oRS = ObjCreate("ADODB.Recordset")
$oConn.Open ("DSN={PostgreSQL};UID=odbc_usr;PWD=odbc_usr;Database=Incoming_rates")
;~ $oConn.Open ("ODBC;DRIVER={PostgreSQL};DATABASE=Incoming_rates;SERVER=localhost;PORT=5432;Uid=odbc_usr;Pwd=odb


;~ $oRS.Open ($query, $oConn, 1, 3)
$oConn.Execute ($query, $oConn, 1, 3)
$oConn = 0

The error witch i get is

~~_read.au3 (55) : ==> The requested action with this object has failed.:

$oConn.Open ("DSN={PostgreSQL};UID=odbc_usr;PWD=odbc_usr;Database=Incoming_rates")

$oConn.Open ("DSN={PostgreSQL};UID=odbc_usr;PWD=odbc_usr;Database=Incoming_rates")^ ERROR

Pleas help me to sort it out...

Share this post

Link to post
Share on other sites


Maybe have look here on how to set the connection string.


I can't test it because I don't have a postgress DB around.



thanks when i will be at work pc i will try. Is this right?


$oConn.Open ("ODBC;DRIVER={PostgreSQL};DATABASE=Incoming_rates;SERVER=localhost;PORT=5432;Uid=odbc_usr;Pwd=od





$oConn.Open ("ODBC;DRIVER={PostgreSQL};DATABASE=Incoming_rates;SERVER=localhost;PORT=5432;Uid=odbc_usr;Pwd=od



or i just have use?

$oConn.Open ("ODBC;DRIVER={PostgreSQL};HOST=myServerAddress;DB=myDataBase;UID=myUsername;PWD=myPassword;PORT=



$oConn.Open ("DRIVER={PostgreSQL};HOST=myServerAddress;DB=myDataBase;UID=myUsername;PWD=myPassword;PORT=2055;


whre i can read more abuot using object? and this functions?

Thanks to all auto it members for helping each other!!!

I will setup remote access to DB

host: KSG.lt

port: 5432

USR & Pass :odbc_usr

just i don't know can you access from remote ip or is this setuped for local because now I cant get in to Database server just got to firewall and redirected port to postgres server. Try and let me know. By tomorrow morning i will get hands fisical on pc so can do what need.

and you provided with incorrect link :)

this is correct


it not progress its postgresql

Edited by slaughter

Share this post

Link to post
Share on other sites

Thanks all for help or ideas or just saying "make it by your self"



Func postgres_query($query)
$oConn = ObjCreate("ADODB.Connection")
$oRS = ObjCreate("ADODB.Recordset")
$cmd = ObjCreate("ADODB.Command")

$line2 =    "DRIVER={PostgreSQL ANSI};DATABASE=Incoming_rates;" & _
            "SERVER=localhost;PORT=5432;Uid=odbc_usr;" & _
            "Pwd=odbc_usr;" & _ 
            "A0=0;A1=6.4;A2=0;A3=0;A4=0;A5=0;A6=;A7=100;A8=4096;A9=0;" & _

$oConn.Open ($line2)
$oConn.execute ($query)


Share this post

Link to post
Share on other sites


Good to see you got if running. :D

Seems that very few people use a postgress DB.

Otherwise the repons would have been greater.



PostGres is not popular among simple ussers. But when your database goes up milion entries its becoming dificult to control it and under MySQL or MS acces

PostGress hve tons of tools and functions and is one of fastest SQL engines on my test MYSQL vas faster on short queris with not much returned object but post gres took over on higher level. PostGress was faster on long queris and was returing large objects faster.

PostGress ir more for busnies

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...