Database Management Using the Properties Window
1 answer
This is a big old topic, but below are some simple examples for reading 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 with any database that you have a driver on, but if you are using a system that supports stored procedures, you are better off using these ...
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
+1
source to share