Article ID: 181782
Article Last Modified on 5/17/2007
Query Name Table Criteria On Field Datatype ------------------------------------------------------------ ProductsByID Products [ProductID] ProductID Integer CustomerByID Customers [CustomerID] CustomerID Text
Button Name Caption
---------------------------------------------------------
Command1 cmdNumeric Numeric Parameter
Command2 cmdText Text Parameter
Command3 cmdParameters Determine Parameter Properties
Dim Conn As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim Cmd1 As New ADODB.Command
Dim Cmd2 As New ADODB.Command
Dim Rs As New ADODB.Recordset
Private Sub Form_Load()
Dim strConn As String
'Change the DSN to match your settings.
strConn = "DSN=dsnAccess;"
With Conn
.CursorLocation = adUseClient
.ConnectionString = strConn
.Open
End With
End Sub
Private Sub cmdNumeric_Click()
'Passes a Numeric parameter to a Microsoft Access 97 QueryDef
'that is based on the Products table. The parameter is on the
'ProductID field.
With Cmd
Set .ActiveConnection = Conn
.CommandText = "Productsbyid"
.CommandType = adCmdStoredProc
'ADO Numeric Datatypes are very particular
.Parameters.Append .CreateParameter("paramProdID", _
adSmallInt, _
adParamInput, _
2) 'Works without a Size
End With
Cmd.Parameters("paramProdID") = 3
'OR
'Cmd.Parameters(0) = 3
Rs.Open Cmd, , adOpenStatic, adLockReadOnly
Debug.Print Rs(0), Rs(1), Rs(2)
Rs.Close
End Sub
Private Sub cmdText_Click()
'Passes a Text parameter to a Microsoft Access 97 QueryDef that
'is based on the Customers table. The parameter is on the
'CustomerID field.
With Cmd1
Set .ActiveConnection = Conn
.CommandText = "Customerbyid"
.CommandType = adCmdStoredProc
'Can use either adVarChar or adChar dataType
.Parameters.Append .CreateParameter("paramCustID", _
adVarChar, _
adParamInput, _
5) 'needs Size to work
End With
Cmd1.Parameters("paramCustID") = "COMMI"
Rs.Open Cmd1, , adOpenStatic, adLockReadOnly
Debug.Print Rs(0), Rs(1), Rs(2)
Rs.Close
End Sub
Private Sub cmdParameters_Click()
'The purpose of this procedure is to determine the
'properties of a parameter.
'
With Cmd2
Set .ActiveConnection = Conn
.CommandText = "ProductsbyID"
.CommandType = adCmdStoredProc
End With
Cmd2.Parameters.Refresh
Debug.Print "The parameter properties for ProductsbyID are: " _
& vbCrLf _
& "Name: " & Cmd2.Parameters(0).Name & vbCrLf _
& "Type: " & Cmd2.Parameters(0).Type & vbCrLf _
& "Direction: " & Cmd2.Parameters(0).Direction & vbCrLf _
& "Size: " & Cmd2.Parameters(0).Size
Debug.Print "-------------"
With Cmd2
Set .ActiveConnection = Conn
.CommandText = "CustomerbyID"
.CommandType = adCmdStoredProc
End With
Cmd2.Parameters.Refresh
Debug.Print "The parameter properties for CustomerbyID are: " _
& vbCrLf _
& "Name: " & Cmd2.Parameters(0).Name & vbCrLf _
& "Type: " & Cmd2.Parameters(0).Type & vbCrLf _
& "Direction: " & Cmd2.Parameters(0).Direction & vbCrLf _
& "Size: " & Cmd2.Parameters(0).Size
End Sub
Additional query words: vbwin kbdse
Keywords: kbhowto kbjet KB181782