1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
<!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>
id int4,<br>
data text,<br>
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=<MyDataSourceName>;" & _
"UID=<MyUsername>;" & _
"PWD=<MyPassword>;" & _
"Database=<MyDatabaseName>"
'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>
|