Public Sub fnModifyParameterOfQuery(strQueryName As String, strParameterName As String, v As Variant)
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim strSelect As String
Dim strParameter As String
Dim strSQL As String
Dim nNumStartParam As Integer
Dim nNumEndParam As Integer
Dim iLoop As Integer
Dim arrParameters() As String
Set db = CurrentDb
Set qd = db.QueryDefs(strQueryName)
strSQL = qd.SQL
nNumStartParam = InStr(strSQL, "PARAMETERS")
If nNumStartParam <> 0 Then
' there is parameter
nNumEndParam = InStr(strSQL, "SELECT")
strSelect = Mid(strSQL, nNumEndParam, Len(strSQL))
strParameter = Left(strSQL, nNumEndParam - 1)
strParameter = Replace(strParameter, "PARAMETERS", "")
strParameter = Replace(strParameter, ";", "")
arrParameters = Split(strParameter, ",")
For iLoop = LBound(arrParameters) To UBound(arrParameters)
If InStr(arrParameters(iLoop), strParameterName) Then
If VarType(v) = vbInteger Or VarType(v) = vbLong Then
arrParameters(iLoop) = arrParameters(iLoop) & " = " & v
Else
arrParameters(iLoop) = arrParameters(iLoop) & " = '" & v & "'"
End If
End If
Next iLoop
strSQL = "Parameters "
For iLoop = LBound(arrParameters) To UBound(arrParameters)
strSQL = strSQL & arrParameters(iLoop) & ","
Next iLoop
strSQL = strSQL & ";"
strSQL = Replace(strSQL, ",;", ";")
strSQL = strSQL & strSelect
End If
qd.SQL = strSQL
Set qd = Nothing
Set db = Nothing
End Sub