Created
September 29, 2016 02:29
-
-
Save adamnew123456/2471c59a2454d675f6786d8461911c26 to your computer and use it in GitHub Desktop.
Prepared Statements in VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Creates a prepared statement and populates its parameters | |
' | |
' Usage: | |
' | |
' Dim stmt as QueryDef | |
' Set stmt = PrepareStatement(CurrentDb, "SELECT * FROM T WHERE A = [aval] AND B = [bval]", _ | |
' "aval", 42, _ | |
' "bval", "Carrot Cake") | |
' | |
Public Function PrepareStatement(db As Database, sql As String, ParamArray params()) As QueryDef | |
Dim query As QueryDef | |
Set query = db.CreateQueryDef(vbNullString, sql) | |
Dim i As Integer | |
For i = LBound(params) To UBound(params) Step 2 | |
query.Parameters(params(i)).Value = params(i + 1) | |
Next | |
Set PrepareStatement = query | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment