tags:

views:

34

answers:

1

Please enumerate the procedure in which a visual basic program can access a database using data control and Active x data object (ADO)?

A: 

This is a big old topic but below are a couple of simple examples to read through a recordset...

Dim con As New ADODB.Connection
con.connectionstring = "My Connection String" -- see ConnectionStrings.com for examples

Dim rs As New ADODB.Recordset
con.Open
rs.Open "SELECT name FROM MyTable", con, adOpenForwardOnly, adLockReadOnly

Do While Not rs.EOF
    Debug.Print rs.fields("name")
rs.movenext
Loop

rs.Close
Set rs = Nothing
con.Close
Set con = Nothing

This will work on any database that you have a driver for but if your using a system that supports stored procedures your better off using those...

Dim con As New ADODB.Connection
con.ConnectionString = "My Connection String" -- see ConnectionStrings.com for examples

Dim cmd As New ADODB.Command
cmd.CommandText = "MySpName"
cmd.CommandType = adCmdStoredProc

Dim param1 As New ADODB.Parameter
With param1
    .Name = "@MyParam"
    .Type = adInteger
    .Direction = adParamInput
    .Value = 10
End With

cmd.Parameters.Append param1
Set param1 = Nothing

Dim rs As ADODB.Recordset
con.Open
cmd.ActiveConnection = con

Set rs = cmd.Execute

Do While Not rs.EOF
    Debug.Print rs.Fields("name")
rs.movenext
Loop

rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
Rich Andrews