| re: How to pass date parameter into Oracle SQL string in VBA
I just managed to figure out the way to pass a parameter to the SQL statement via VBA. It doesn't involve the standard Oracle parameter indicator, which is what was driving me mad. The secret is to use a question mark for each parameter, then set the parameter value in order of appearance in the SQL statement.
The following is the amended code:
.CommandText = "SELECT T104F005_EMPLOYEE_NO, " & _
"to_char(T104F025_DATE_EFFECTIVE, 'DD/MM/YYYY'), " & _
"from DATABASE.T104_EMPLOYMENT_HISTORY " & _
"where T104F025_DATE_EFFECTIVE BETWEEN ? AND ? " & _
"order by T104F005_EMPLOYEE_NO "
Set MyParameter = .CreateParameter("StartDate", adDBDate, dParamInput, , StartDate)
.Parameters.Append MyParameter ' this sets the value for the 1st parameter
Set MyParameter = .CreateParameter("EndDate", adDBDate, dParamInput, , EndDate)
.Parameters.Append MyParameter ' this sets the value for the 2nd parameter
StartDate and EndDate are date variables which have been set to certain dates (using the DD-MMM-YYYY format). MyParameter is a ADODB.Parameter variable.
Cheers
|