From dan@dpci.us Sat Sep 6 14:54:39 2003 X-Original-To: pgsql-odbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2587AD1BA45 for ; Sat, 6 Sep 2003 14:54:39 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 86910-02 for ; Sat, 6 Sep 2003 11:54:11 -0300 (ADT) Received: from hall.mail.mindspring.net (hall.mail.mindspring.net [207.69.200.60]) by svr1.postgresql.org (Postfix) with ESMTP id BEDA7D1BA68 for ; Sat, 6 Sep 2003 11:54:03 -0300 (ADT) Received: from h-68-165-133-19.dnvtco56.covad.net ([68.165.133.19] helo=server) by hall.mail.mindspring.net with smtp (Exim 3.33 #1) id 19veS0-0001xq-00 for pgsql-odbc@postgresql.org; Sat, 06 Sep 2003 10:54:08 -0400 Message-ID: <000a01c37487$a661cc60$0100005a@server> From: "DanPerlman" To: Subject: ODBC in VB6 Date: Sat, 6 Sep 2003 09:00:44 -0600 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0007_01C37455.5AE27AA0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.6 tagged_above=0.0 required=5.0 tests=HTML_30_40 X-Spam-Level: X-Archive-Number: 200309/32 X-Sequence-Number: 3230 This is a multi-part message in MIME format. ------=_NextPart_000_0007_01C37455.5AE27AA0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I have never used ODBC in a VB6 program before. I am trying to write some simple code to append data from an access 2k tabl= e to a postgres table. My DSN works with access. I am getting a "type mismatch" error when trying to open the postgres db. Thanks, Dan Private Sub CommandStart_Click() Dim Pricebooks As Database Dim Bonneville As Database Dim WrkSpc As Workspace Dim Apb As Recordset Dim Bpb As Recordset Dim i As Integer 'Open Access table. Set Pricebooks =3D OpenDatabase(App.Path & "\Pricebooks.mdb") Set Apb =3D Pricebooks.OpenRecordset("pricebook", dbOpenTable) 'Open Postgres table Set Bonneville =3D OpenDatabase(Bonneville, False, "ODBC;DSN=3DPostgresSQL3= 0;UID=3Dpostgres;PWD=3Dpostgres;") Set Bpb =3D Bonneville.OpenRecordset(public_papricebook, dbOpenDynamic) TextID.Text =3D Apb("pricebookid") & "" TextID.Refresh End Sub ------=_NextPart_000_0007_01C37455.5AE27AA0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi,
 
I have never used ODBC in a VB6 progr= am=20 before.
I am trying to write some simple code to a= ppend=20 data from an access 2k table to a postgres table.
My DSN works with access.
I am getting a "type mismatch" error when = trying to=20 open the postgres db.
 
Thanks,
 
Dan
 
Private Sub CommandStart_Click()
 
Dim Pricebooks As Database
Dim Bonnevil= le As=20 Database
Dim WrkSpc As Workspace
Dim Apb As Recordset
Dim Bpb As= =20 Recordset
Dim i As Integer
 
'Open Access table.
Set Pricebooks =3D= =20 OpenDatabase(App.Path & "\Pricebooks.mdb")
Set Apb =3D=20 Pricebooks.OpenRecordset("pricebook", dbOpenTable)
 
'Open Postgres table
Set Bonneville =3D= =20 OpenDatabase(Bonneville, False,=20 "ODBC;DSN=3DPostgresSQL30;UID=3Dpostgres;PWD=3Dpostgres;")
Set Bpb =3D= =20 Bonneville.OpenRecordset(public_papricebook, dbOpenDynamic)
 
TextID.Text =3D Apb("pricebookid") &= =20 ""
TextID.Refresh
 

End Sub
------=_NextPart_000_0007_01C37455.5AE27AA0-- From aayers@eldocomp.com Mon Sep 8 18:13:26 2003 X-Original-To: pgsql-odbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4FF00D1B4E2 for ; Mon, 8 Sep 2003 18:13:26 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 90276-10 for ; Mon, 8 Sep 2003 15:12:56 -0300 (ADT) Received: from ecintweb.eldocomp.com (smtp.eldocomp.com [205.159.99.8]) by svr1.postgresql.org (Postfix) with ESMTP id 1056AD1B4E7 for ; Mon, 8 Sep 2003 15:12:54 -0300 (ADT) Received: by ecintweb.eldocomp.com with XWall v3.27j ; Mon, 8 Sep 2003 11:12:38 -0700 From: Andrew Ayers To: DanPerlman Cc: "pgsql-odbc@postgresql.org" Subject: Re: ODBC in VB6 Date: Mon, 8 Sep 2003 11:12:43 -0700 X-Assembled-By: XWall v3.27j Message-ID: <3F5CC69B.2030305@eldocomp.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/34 X-Sequence-Number: 3232 DanPerlman wrote: > Hi, >=20=20 > I have never used ODBC in a VB6 program before. > I am trying to write some simple code to append data from an access 2k=20 > table to a postgres table. > My DSN works with access. > I am getting a "type mismatch" error when trying to open the postgres db. First off, for a good answer I think we need some more information,=20 specifically what version of PostgreSQL you are using, as well as what=20 version of the ODBC driver you are using. Also, it would be helpful to=20 know what your settings are on the driver. With that said, let me outline what has worked for me. I am currently using version 7.3.2 of PostgreSQL running under Solaris.=20 I am using version 7.03.01 of the ODBC driver on my XP workstation. What I have found to work best with my VB application is to use ADO,=20 instead of DAO, for working with the PostgreSQL database. There are many=20 reasons behind this, which if you want to dig in the ODBC and GENERAL=20 mailing list archives, you can see what I went through to get to this point. Along with DAO, I use a DSN-less connection. This is mainly because my=20 application is currently 2-tier, and I don't want to have to set up, or=20 have set up, 75-100 ODBC DSNs on the client machines. If your=20 application is a web application or something similar (that is, the=20 database isn't likely to have multiple clients connecting, or the pool=20 of machines connecting is small, or non-migrating), then a "hard" DSN=20 connection would likely be preferable. My code for connecting with and updating the database then is as follows=20 - first you need to set up your connection: --- Dim conn As New ADODB.Connection Set conn =3D New ADODB.Connection conn.Open "dsn_name" ' DSN Connection conn.Open "driver=3D{PostgreSQL};server=3Dxxxx;port=3Dyyyy; _ database=3Dzzzz;uid=3D;pwd=3D;ksqo=3D;" ' DSN-less Connection --- A few words here: Choose either the DSN or DSN-less connection (comment out one of the=20 lines, or only type one in). For the DSN connection, set the name of the=20 DSN you have defined in the connection string you pass. For the DSN-less=20 connection, you will need to sent the server name (xxxx), the port it is=20 on (yyyy), the database name (zzzz), and perhaps the user name (uid),=20 and password (pwd). Leave ksqo set to nothing. This variable is the=20 "keyset query optimization" flag - I found that setting it caused=20 problems (whether in the driver for a DSN connection, or in the=20 connection string for a DSN-less connection). Also, note that I haven't defined a Workspace. There is a lot of code=20 floating around on the internet about ADO, and nearly every example=20 shows using a workspace. I am not certain why this is, but I found that=20 the code I was using worked fine without one, and there didn't seem to=20 be any need to include the extra code (I tried it out, it didn't seem to=20 help nor hinder any). If you feel you need it, by all means include it. Now, you need to get the recordset: --- Dim rs As New ADODB.Recordset Set rs =3D New ADODB.Recordset rs.CursorLocation =3D adUseServer rs.Open strSqlStatement, conn, adOpenDynamic, _ adLockOptimistic, adCmdText --- Here we set up a new ADO recordset object, then using the connection=20 object (conn) and a passed SQL statement, we select our recordset. You=20 will notice I am doing optimistic locking. There are other options you=20 can pass (for example, if all you are doing is reading, you might pass=20 adLockReadOnly instead). A few more notes: wildcard characters in SQL are different for Access=20 vs. PostgreSQL. Keep this in mind if you are accustomed to the wildcard=20 characters for Access. Another thing to note is that if you are doing all of your updates via=20 SQL, this is all the code you are likely to need (ie, if you do SQL=20 INSERTs and DELETEs). In fact, if you were developing a new application,=20 I would reccommend doing this, instead of programmatically from the=20 application. It should allow for more portable code, plus you get the=20 database backend to do all the "heavy lifting", among other pluses. If I=20 had the chance to redo my application this way, I would. If not, read on - to programmatically read from the recordset: --- With rs ' If Not (.BOF And .EOF) Then ' Make sure there are records selected ' .MoveLast .MoveFirst ' strData =3D ![field] ' End If ' End With --- To programmatically add to the recordset: --- With rs ' .AddNew ' ![field] =3D strData ' .update ' End With --- To programmatically update the recordset: --- With rs ' If Not (.BOF And .EOF) Then ' Make sure there are records selected ' .MoveLast .MoveFirst ' ![field] =3D strData ' .update ' End If ' End With --- Note that no ".Edit" is needed - in ADO this is unnecessary. To programmatically delete from the recordset: --- With rs ' If Not (.BOF And .EOF) Then ' Make sure there are records selected ' .MoveLast .MoveFirst ' .Delete ' End If ' End With --- There are a few other things I want to note about using ADO (and=20 PostgreSQL). One thing is that in using ADO, for some reason there isn't=20 a way to get a recordcount (at least, as I have set things up). That is,=20 if you try something like "x =3D rs.RecordCount", you will *always* get -1= =20 as the value. IIRC, it has to do with setting the cursor location to=20 that of the server. However, setting it to the client introduces other=20 issues. I have found that the best way to get the record count is to=20 simply do a SQL "SELECT COUNT(*)" with the same criteria as your select.=20 Obviously, this method isn't dynamic (ie, if you add or delete records,=20 the count doesn't change), but it does work. Another thing I have found is that strange things can occur if you=20 attempt to update records within a recordset that is already open and is=20 being updated. That is, if you open the recordset, and in the middle of=20 the update you call a function (whether explicitly, via a timer, or some=20 other method) that selects the any of the same records and updates them,=20 you may get errors by the second update, or when it returns and you try=20 to close the first. With DAO, this isn't a problem. I think it is really=20 bad habits "taught" by DAO - that you shouldn't be able to update the=20 same record everywhere in the application and have the changes reflect=20 everywhere. Not sure... Finally, close the recordset and the database connection when finished: --- rs.Close: Set rs =3D Nothing Set conn =3D Nothing --- Also, if you notice, I don't show using "bound controls" in my examples=20 (as you seem to have been showing in your example). I am of the position=20 of having the application do the work, and I have also found that bound=20 controls, while making initial application development quick, tends to=20 make maintenance more difficult in the future. I have also run across=20 reports on the web about why using bound controls is a bad thing, and=20 the arguments were persuasive. Whether this is a new application or not,=20 I would like to persuade you to read up on this issue, and decide for=20 yourself. I hope this response helps you in some manner. Much of what I have=20 outlined here came from various experimentation on my own, as well as a=20 ton of insight and help from both the ODBC and GENERAL PostgreSQL lists.=20 Even so, I am sure I am missing things, but so far the above methods=20 have worked within my application. Good luck! Andrew L. Ayers Phoenix, Arizona -- CONFIDENTIALITY NOTICE -- This message is intended for the sole use of the individual and entity to w= hom it is addressed, and may contain information that is privileged, confid= ential and exempt from disclosure under applicable law. If you are not the = intended addressee, nor authorized to receive for the intended addressee, y= ou are hereby notified that you may not use, copy, disclose or distribute t= o anyone the message or any information contained in the message. If you ha= ve received this message in error, please immediately advise the sender by = reply email, and delete the message. Thank you.