head	1.2;
access;
symbols
	REL-09_01_0200:1.2
	REL-09_01_0100:1.2
	REL-09_00_0310:1.2
	REL-09_00_0300:1.2
	REL-09_00_0200:1.2
	REL-09_00_0101:1.2
	REL-09_00_0100:1.2
	REL-08_04_0200:1.2
	REL-08_04_0100:1.2
	REL-08_03_0400:1.2
	REL-08_03_0300:1.2
	REL-08_03_0200:1.2
	REL-08_03_0100:1.2
	REL-08_02_0500:1.2
	REL-08_02_0400:1.2
	REL-08_02_0300:1.2
	REL-08_02_0200:1.2
	REL-08_02_0100:1.2
	REL8_1_STABLE:1.2.0.2
	REL-08_01_0200:1.2
	REL-08_01_0102:1.1
	REL-08_01_0101:1.1
	REL-08_01_0100:1.1
	REL-08_01_0006:1.1
	REL-08_01_0005:1.1
	REL-08_01_0004:1.1;
locks; strict;
comment	@# @;


1.2
date	2006.01.06.12.18.28;	author dpage;	state Exp;
branches;
next	1.1;

1.1
date	2005.08.27.19.44.47;	author dpage;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Some minor corrections
@
text
@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>psqlODBC HOWTO - Visual Basic</title>
  </HEAD>

  <body bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#a00000" alink="#0000ff">
  
<h1>psqlODBC HOWTO - Visual Basic</h1>

<p>

<i>
Author: Dave Page (dpage@@postgresql.org)<br>
Release Date: 13 November 2001<br>
Description: Example based Mini-Howto on Accessing PostgreSQL from Visual Basic
</i>
<br><br>
This document provides some sample code to get you started with Visual Basic & PostgreSQL.
<br><br>
Requirements to get the subroutines to work:

<br>
<ul>
<li>Visual Basic 5/6</li>
<li>A reference in the VB project to Microsoft ActiveX Data Objects</li>
<li>A PostgreSQL datasource.</li>
</ul>

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:

<br>
<blockquote>
<code>
CREATE TABLE vbtest(<br>
&nbsp;&nbsp;  id int4,<br>
&nbsp;&nbsp;  data text,<br>
&nbsp;&nbsp;  accessed timestamp<br>
);
</code>
</blockquote> 

<h2>Code</h2>
<blockquote>
<pre>

Sub Main()
Dim cn as New ADODB.Connection
Dim rs as New ADODB.Recordset
 
  'Open the connection
  cn.Open "DSN=&lt;MyDataSourceName&gt;;" &amp; _
          "UID=&lt;MyUsername&gt;;" &amp; _
          "PWD=&lt;MyPassword&gt;;" &amp; _
          "Database=&lt;MyDatabaseName&gt;"
 
  '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 
  'directions. 
  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")
    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.Requery

  '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

</pre>
</blockquote>
</p>

<h2>Useful Functions</h2>
<blockquote>
<pre>

' 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"
  else
    escapeString = "'" & Replace(Replace(vValue, "", ""), "'", "''") & "'" 
  end if

End Function

</pre>
</blockquote>
</p>

</body>
</html>
@


1.1
log
@Move docs into the source tree.
@
text
@d59 3
d66 1
a66 1
  rs.Open "SELECT id, data FROM vbtest", cn, adOpenDynamic
d89 1
a89 1
  rs.Refresh
d131 1
a131 1
</html>@

