Code Snippet "VBA Access"
Parametrisierte Abfrage (Query) in Access aus VBA-Code aufrufen (ADO)
'Parametrisierte Query in Access mit Recordset
Sub testParametersWithRS()
Dim intF As Long
Dim intFields As Long
Dim intR As Long
Dim intRecords As Long
Dim objCmd As New ADODB.Command
Dim objCon As New ADODB.Connection
Dim objPrm As ADODB.Parameter
Dim objRS As ADODB.Recordset
Dim strParameterName As String
Dim strQueryName As String
Dim strText As String
Set objCon = CurrentProject.Connection
objCmd.CommandType = adCmdText
objCmd.CommandText = "SELECT * FROM xqryparameter"
objCmd.ActiveConnection = objCon
'Hier wird der Parameter zugewiesen
' Name: Name des Parameters
' Type: Datentyp des Parameters
' Direction: geht rein -> adParamInput; kommt raus -> adParamOutput
' Size: maximale Länge für den Parameterwert in Zeichen oder Bytes
' Value: Wert des Parameters
objCmd.Parameters.Append objCmd.CreateParameter( _
"@KatNr", adInteger, adParamInput, , 27)
Set objRS = objCmd.Execute
Do While Not objRS.EOF
Debug.Print objRS("Kategorie")
objRS.MoveNext
Loop
objRS.Close
Set objRS = Nothing
objCon.Close
Set objCon = Nothing
End Sub