Gemeinsam zu mehr Effizienz in der Anwendungserstellung
K |
K |
||
Zeile 41: | Zeile 41: | ||
"abc", 123, Now()) | "abc", 123, Now()) | ||
</source> | </source> | ||
+ | |||
+ | |||
+ | === Variante für gespeicherter Abfrage === | ||
+ | <source> | ||
+ | Public Function ExecuteParamQdf(ByVal QueryDefName As String, _ | ||
+ | ParamArray QueryParams() As Variant) As Long | ||
+ | |||
+ | Dim qdf As DAO.QueryDef | ||
+ | |||
+ | Set qdf = CurrentDb.QueryDefs(QueryDefName) | ||
+ | |||
+ | Dim i As Long | ||
+ | For i = 0 To UBound(QueryParams) Step 2 | ||
+ | qdf.Parameters(QueryParams(i)) = QueryParams(i + 1) | ||
+ | Next | ||
+ | qdf.Execute dbFailOnError | ||
+ | ExecuteParamQdf = qdf.RecordsAffected | ||
+ | qdf.Close | ||
+ | |||
+ | End Function | ||
+ | </source> | ||
+ | |||
+ | ==== Verwendung ==== | ||
+ | SQL der gespeicherten Abfrage: <code>insert into Tabelle (T, Z, D) Values ([P1], [P2], [P3])</code> | ||
+ | |||
+ | <source> | ||
+ | anzahlDS = ExecuteParamSQL("GespeicherteAbfrage" & _ | ||
+ | "P1", "abc", _ | ||
+ | "P2", 123, _ | ||
+ | "P3", Now()) | ||
+ | </source> | ||
+ | |||
{{Hierarchy footer}} | {{Hierarchy footer}} |
Programmier-Konzepte |
|
Der unten angeführte Code zeigt nur ein Prinzip und stellt keine fertige Lösung dar.
Eine Insert- oder Update-Anweisung soll per VBA zusammengesetzt und mit Werten aus Variablen versorgt werden.
Public Function ExecuteParamSQL(ByVal SqlText As String, _ ParamArray QueryParams() As Variant) As Long Dim qdf As DAO.QueryDef Set qdf = CurrentDb.CreateQueryDef("", SqlText) Dim i As Long For i = 0 To UBound(QueryParams) qdf.Parameters(i) = QueryParams(i) Next qdf.Execute dbFailOnError ExecuteParamSQL = qdf.RecordsAffected qdf.Close End Function
anzahlDS = ExecuteParamSQL("Parameters P1 Text(255), P2 int, P3 date; " & _ "insert into Tabelle (T, Z, D) Values ([P1], [P2], [P3])", _ "abc", 123, Now())
Public Function ExecuteParamQdf(ByVal QueryDefName As String, _ ParamArray QueryParams() As Variant) As Long Dim qdf As DAO.QueryDef Set qdf = CurrentDb.QueryDefs(QueryDefName) Dim i As Long For i = 0 To UBound(QueryParams) Step 2 qdf.Parameters(QueryParams(i)) = QueryParams(i + 1) Next qdf.Execute dbFailOnError ExecuteParamQdf = qdf.RecordsAffected qdf.Close End Function
SQL der gespeicherten Abfrage: insert into Tabelle (T, Z, D) Values ([P1], [P2], [P3])
anzahlDS = ExecuteParamSQL("GespeicherteAbfrage" & _ "P1", "abc", _ "P2", 123, _ "P3", Now())