CodeItBetter Programming Another VB Programming Blog

How to Connect to a data source using ADO and retrieving the data.

Posted on January 4, 2009
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
'Database - How to Connect to a data source using ADO and retrieving the data.
'Create a reference to 'Microsoft ActiveX Data Object 2.0 Library'

Private Sub Form_Load()
    'Declare the connection and recordset objects
    Dim cnData As Connection
    Dim rs As Recordset
 
    'Instantiate the objects
    Set cnData = New Connection
    Set rs = New Recordset
 
    'Set the connection string
    cnData.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=<<PATH TO DATABASE>>"
    cnData.Open
 
    'Code to test the connection
    If cnData.State = adStateOpen Then MsgBox "Connection good."
 
    'Get data from the database connection
    rs.Open "<<SQL STATEMENT>>", cnData
 
    'For testing purposes, write to debug window
    Debug.Print rs!<<DATABASE_COLUMN_FIELD_NAME>>
 
    'Make sure to close the connection and release cnData
    cnData.Close
    Set cnData = Nothing
End Sub
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.