Skip to content

Instantly share code, notes, and snippets.

@greenkey
Last active February 25, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenkey/b90c4d58794dae15f3f3 to your computer and use it in GitHub Desktop.
Save greenkey/b90c4d58794dae15f3f3 to your computer and use it in GitHub Desktop.
VBScript, ADO and query parameters (question marks), a handy function
' inspired by:
' http://stackoverflow.com/questions/2557606/how-do-i-associate-parameters-to-command-objects-in-ado-with-vbscript
' http://stackoverflow.com/questions/10352211/vba-ado-connection-and-query-parameters
' The function takes three parameters: ADOConnection; sql (string); params, an Array() of parameters
Function sqlOperationParametric(ADOConnection,sql,params)
Dim Cm, Pm, i, p
Set Cm = CreateObject("ADODB.Command")
With Cm
.ActiveConnection = ADOConnection
.CommandText = sql
.CommandType = adCmdText
For i=0 To UBound(params)
p = params(i)
Set Pm = .CreateParameter()
With Pm
.Direction = adParamInput
Select Case VarType(p)
Case vbString
.Type = adVarChar
.Size = Len(p)
.Value = p
Case vbInteger, vbLong, vbSingle, vbDouble
.Type = adDouble
Pm.Value = p
Case vbDate
.Type = adDBTimeStamp
.Value = p
' sure there should be other cases, but these are the most used
End Select
End With
.Parameters.Append Pm
Next
Set sqlOperationParametric = .Execute
End With
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment