X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7304D53B6D; Sat, 30 Apr 2005 14:56:40 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80554-03; Sat, 30 Apr 2005 17:56:35 +0000 (GMT) Received: from imo-m27.mx.aol.com (imo-m27.mx.aol.com [64.12.137.8]) by svr1.postgresql.org (Postfix) with ESMTP id EBB8653BAE; Sat, 30 Apr 2005 14:56:33 -0300 (ADT) Received: from Typing80wpm@aol.com by imo-m27.mx.aol.com (mail_out_v38_r1.7.) id r.86.273fdd3b (4004); Sat, 30 Apr 2005 13:56:27 -0400 (EDT) From: Typing80wpm@aol.com Message-ID: <86.273fdd3b.2fa520cb@aol.com> Date: Sat, 30 Apr 2005 13:56:27 EDT Subject: VB questions re: Postgresql, Connect...etc To: jeff_eckermann@yahoo.com Cc: pgsql-general@postgresql.org, pgsql-odbc@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="-----------------------------1114883787" X-Mailer: 9.0 SE for Windows sub 5012 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.618 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_30_40, HTML_MESSAGE, NO_REAL_NAME X-Spam-Level: X-Archive-Number: 200504/1429 X-Sequence-Number: 77342 -------------------------------1114883787 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Thanks Jeff, and all, for suggestions: _http://gborg.postgresql.org/project/psqlodbc/genpage.php?howto-visualbasic_ (http://gborg.postgresql.org/project/psqlodbc/genpage.php?howto-visualbasic) I have Visual Basic 6.0 The code at this page looks like exactly what I need. I anticipate my problems as follows: 1:) A reference in the VB project to Microsoft ActiveX Data Objects I know that I must somehow go to tools and check off certain tools for the project so that the VBA code will be able to make the odbc connection. I am assuming that the above "Microsoft ActiveX Data Objects" will somehow guide me to checking the correct box and adding the correct object(s) to the project. 2:) I always have difficulties with statements like this: cn.Open "DSN=;" & _ "UID=;" & _ "PWD=;" & _ "Database=" I know, for example that my user name will be neil and my password will be "password" , based upon an exercise I did in my book on Postgresql. I assume that my database name will be "bpsimple" which is the name of the database created by the example, for user "neil". I know that I can go into windows, into the ODBC administration, and create a new DSN name, and I shall be offered some choices like "system", "user", "file",.... but the DSN name is where I run into confusion and trouble. It will SAVE the DSN name someplace under the name of my choosing, and I know that MSAccess displays such names to me during the dialogue to link to an external datasource. 3.) Dim cn as New ADODB.Connection Dim rs as New ADODB.Recordset I know that, if I do not add the correct objects/modules to my vb project, and get the DSN correct, that I shall have problems with Dim cn as New ADODB.Connection. But I shall begin to experiment with all these things, for the script example is exactly what I need, IF I can get it to work and talk to the Postgresql server through odbc under windows. Hence, I shall create a simple .exe project in VB with a window and a button. I shall endeavor to add the objects to that project with pertain to "Microsoft ActiveX Data Objects". For example, here comes the maddening part for me right now. I have launched VB, and started a project and placed a button on the form, but NOW, I must remember from tutorials what to click on to ADD the Microsoft ActvieX Data Objects I just went to Projects, added a Dataenvironment , am looking at the Datalink Properties, have chosen PostgreSQL, added user name and password, which is postgres and my password.... it verifies that the connection works, but it does not work with the INITIAL CATALOG TO USE field, nor am i quite certain what to put in such a field. WAIT....!!! now, upon further inspection, I PAGE DOWN and see that there is a POSTGRES OLE provider... so obviously I should choose that (i think)...so NOW it asks for DATASOURCE and LOCATION... so, do I put "localhost" or some ip address... or what..... SOOO... I go back to my ODBC PostgreSQL choice,... and see that connection works.... AND, if I take the choice that says BUILD CONNECTION STRING, then it returns: DRIVER={PostgreSQL};DATABASE=bpsimple;SERVER=localhost;PORT= 5432;UID=neil;PWD=password;ReadOnly=0;Protocol=6.4;FakeOidInd ex=0;ShowOidColumn=0;RowVersioning=0;ShowSystemTables=0;Con nSettings=;Fetch=100;Socket=8192;UnknownSizes=0;MaxVarcharSiz e=254;MaxLongVarcharSize=8190;Debug=0;CommLog=0;Optimizer= 1;Ksqo=1;UseDeclareFetch=0;TextAsLongVarchar=1;UnknownsAsLon gVarchar=0;BoolsAsChar=1;Parse=0;CancelAsFreeStmt=0;ExtraSysTa blePrefixes=dd_;LFConversion=1;UpdatableCursors=0;DisallowPrema ture=0;TrueIsMinus1=0;BI=0;ByteaAsLongVarBinary=0;UseServerSide Prepare=0 So this represents great learning progress for me, because for the first time, I have found a way to build that complex string, which might be needful in VBA script. But, now I must understand how to translate all of this into a DSN name for the vba script example, DSN= Now, the DATASOURCE NAME seems to be PostgreSQL in the Data Link Properties dialogue, and yet, in the ODBC windows administrator, FILE DSN, I have a definition which I saved as "bpsimple" So, how to fill in properly: 'Open the connection cn.Open "DSN=bpsimple;" & _ "UID=neil;" & _ "PWD=password;" & _ "Database=bpsimple" ???????????????????????????????????????? or should it be: 'Open the connection cn.Open "DSN=PostgreSQL;" & _ "UID=neil;" & _ "PWD=password;" & _ "Database=bpsimple" ?????????? I shall experiment over and over with Windows ODBC to define a DSN and store it in different places, until I get it to work. And I shall post my problems to these groups. Thanks for the help! =============== code example Sub Main() Dim cn as New ADODB.Connection Dim rs as New ADODB.Recordset 'Open the connection cn.Open "DSN=;" & _ "UID=;" & _ "PWD=;" & _ "Database=" '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 'directions. rs.Open "SELECT id, data FROM vbtest", cn, adOpenDynamic '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") rs.Update rs.MoveNext Wend 'Add a new record to the recordset rs.AddNew rs!id = 76 rs!data = 'More random data' rs!accessed = Format(Now, "yyyy-MM-dd hh:mm:ss") rs.Update '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... rs.Refresh 'Get the record count rs.MoveLast rs.MoveFirst MsgBox rs.RecordCount & " Records are in the recordset!" 'Cleanup If rs.State <> adStateClosed Then rs.Close Set rs = Nothing If cn.State <> adStateClosed Then cn.Close Set cn = Nothing End Sub =================end code example -------------------------------1114883787 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable
Thanks Jeff, and all, for suggestions:
 
http://gborg.postgresql.org/project/psqlodbc/genpage.php?howto-visua= lbasic
 
 
I have Visual Basic 6.0
 
The code at this page looks like exactly what I need.
 
I anticipate my problems as follows:
 
1:) A reference in the VB project to Microsoft ActiveX Data Objects I k= now=20 that I must somehow go to tools and check off certain tools for the project=20= so=20 that the VBA code will be able to make the odbc connection.  I am assum= ing=20 that the above "Microsoft ActiveX Data
 
Objects" will somehow guide me to checking the correct box and adding t= he=20 correct object(s) to the project.
 
2:) I always have difficulties with statements like this:
cn.Open=20 "DSN=3D<MyDataSourceName>;" &=20 _
         =20 "UID=3D<MyUsername>;" &=20 _
         =20 "PWD=3D<MyPassword>;" &=20 _
         =20 "Database=3D<MyDatabaseName>"
 
 
I know, for example that my user name will be neil and my password will= be=20 "password" , based upon an exercise I did in my book on Postgresql. &nb= sp;=20 I assume that my database name will be "bpsimple"  which is the name of= the=20 database created by the example, for user
"neil".   I know that I can go into windows, into the ODBC=20 administration, and create a new DSN name, and I shall be offered some choic= es=20 like "system", "user", "file",.... but the DSN name is where I run into=20 confusion and trouble. It will SAVE the DSN name someplace under the name of= my=20 choosing, and I know that MSAccess displays such names to me during the dial= ogue=20 to link to an external datasource.
 
3.) Dim cn as New ADODB.Connection
Dim rs as New=20 ADODB.Recordset
 
I know that, if I do not add the correct=20 objects/modules to my vb project, and get the DSN correct, that I shall have= =20 problems with Dim cn as New ADODB.Connection.
 
But I shall begin to experiment with all these things, for the script=20 example is exactly what I need, IF I can get it to work and talk to the=20 Postgresql server through odbc under windows.
 
Hence, I shall create a simple .exe project in VB with a window and a=20 button. I shall endeavor to add the objects to that project with pertain to=20 "Microsoft ActiveX Data Objects".
 
For example, here comes the maddening part for me right now. I have=20 launched VB, and started a project and placed a button on the form, but NOW,= I=20 must remember from tutorials what to click on to ADD the Microsoft ActvieX D= ata=20 Objects
 
I just went to Projects, added a Dataenvironment , am looking at the=20 Datalink Properties, have chosen PostgreSQL, added user name and password, w= hich=20 is postgres and my password.... it verifies that the connection works, but i= t=20 does not work with the INITIAL CATALOG TO USE field, nor am i quite certain=20= what=20 to put in such a field. WAIT....!!!
now, upon further inspection, I PAGE DOWN and see that there is a POSTG= RES=20 OLE provider... so obviously I should choose that (i think)...so NOW it asks= for=20 DATASOURCE and LOCATION... so, do I put "localhost" or some ip address... or= =20 what.....
SOOO... I go back to my ODBC PostgreSQL choice,... and see that connect= ion=20 works.... AND, if I take the choice that says BUILD CONNECTION STRING, then=20= it=20 returns:
 
DRIVER=3D{PostgreSQL};DATABASE=3Dbpsimple;SERVER=3Dlocalhost;PORT=3D
5432;UID=3Dneil;PWD=3Dpassword;ReadOnly=3D0;Protocol=3D6.4;FakeOidInd
ex=3D0;ShowOidColumn=3D0;RowVersioning=3D0;ShowSystemTables=3D0;Con
nSettings=3D;Fetch=3D100;Socket=3D8192;UnknownSizes=3D0;MaxVarcharSiz
e=3D254;MaxLongVarcharSize=3D8190;Debug=3D0;CommLog=3D0;Optimizer=3D
1;Ksqo=3D1;UseDeclareFetch=3D0;TextAsLongVarchar=3D1;UnknownsAsLon
gVarchar=3D0;BoolsAsChar=3D1;Parse=3D0;CancelAsFreeStmt=3D0;ExtraSysTa<= /DIV>
blePrefixes=3Ddd_;LFConversion=3D1;UpdatableCursors=3D0;DisallowPrema
ture=3D0;TrueIsMinus1=3D0;BI=3D0;ByteaAsLongVarBinary=3D0;UseServerSide=
Prepare=3D0
 
 
So this represents great learning progress for me, because for the firs= t=20 time, I have found a way to build that complex string, which might be needfu= l in=20 VBA script. But, now I must understand how to translate all of this into a D= SN=20 name for the vba script example, DSN=3D<MyDataSourceName> Now, the=20 DATASOURCE NAME seems to be PostgreSQL in the Data Link Properties dialogue,= and=20 yet, in the ODBC windows administrator,
FILE DSN, I have a definition which I saved as "bpsimple"
 
So, how to fill in properly:
 
  'Open the connection
  cn.Open "DSN=3Dbpsimple;" &=20 _
          "UID=3Dneil;" &a= mp;=20 _
          "PWD=3Dpassword;= "=20 & _
         =20 "Database=3Dbpsimple"
 
????????????????????????????????????????
 
or should it be:
 
  'Open the connection
  cn.Open "DSN=3DPostgreSQL;" &= =20 _
          "UID=3Dneil;" &a= mp;=20 _
          "PWD=3Dpassword;= "=20 & _
         =20 "Database=3Dbpsimple"
 
??????????
 
I shall experiment over and over with Windows ODBC to define a DSN and=20 store it in different places, until I get it to work. And I shall post my=20 problems to these groups.  Thanks for the help!
 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D code example
 
Sub Main()
Dim cn as New ADODB.Connection
Dim rs as New=20 ADODB.Recordset
 
  'Open the connection
  cn.Open=20 "DSN=3D<MyDataSourceName>;" &=20 _
         =20 "UID=3D<MyUsername>;" &=20 _
         =20 "PWD=3D<MyPassword>;" &=20 _
         =20 "Database=3D<MyDatabaseName>"
 
  'For updateable reco= rdsets=20 we would typically open a Dynamic
 
recordset.
  'Forward Only recordsets are much faster but can o= nly=20 scroll forward
 
and
  'are read only. Snapshot recordsets are read only, but=20 scroll in both
  'directions.
  rs.Open "SELECT id, data F= ROM=20 vbtest", cn, adOpenDynamic
 
  'Loop though the recordset an= d=20 print the results
  'We will also update the accessed column, but th= is=20 time access it
 
through
  'the Fields collection. ISO-8601 formatted dates/tim= es=20 are the safest
 
IMHO.
  While Not rs.EOF
    Debug.Print rs!i= d=20 & ": " & rs!data
    rs.Fields("accessed") =3D=20 Format(Now, "yyyy-MM-dd hh:mm:ss")
   =20 rs.Update
    rs.MoveNext
  Wend
 
&nbs= p;=20 'Add a new record to the recordset
  rs.AddNew
  rs!id =3D=20 76
  rs!data =3D 'More random data'
  rs!accessed =3D Format= (Now,=20 "yyyy-MM-dd hh:mm:ss")
  rs.Update
 
  'Insert a new record into the table
  cn.Execute "INSERT= =20 INTO vbtest (id, data) VALUES (23, 'Some random
 
data');"
 
  'Refresh the recordset to get that last record...
 =20 rs.Refresh
 
  'Get the record count
  rs.MoveLast
 =20 rs.MoveFirst
  MsgBox rs.RecordCount & " Records are in the=20 recordset!"
 
  'Cleanup
  If rs.State <> adStateClosed Then=20 rs.Close
  Set rs =3D Nothing
  If cn.State <> adState= Closed=20 Then cn.Close
  Set cn =3D Nothing
End Sub
=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3Dend code=20 example
 
 
-------------------------------1114883787--