So, database hotchpotch.
- I have a MySQL database which has stored procedures.
- I have an Access document which is linked to the MySQL database (I can survey tables) serving as a frontend (with forms and stuff)
- I have successfully created a ODBC Pass Through Query (I think that's what they are called) to call one of the stored procedures from the database. The query in Access actually is just
CALL myProcName;and gives me the results I expect when I call it from VBA withSet Result = CurrentDb.OpenRecordset("myProcName")
What I am trying to achieve is the following:
- Have a stored procedure in MySQL that takes one parameter => check!
- Have that stored procedure working as expected => checked in phpMyAdmin => check!
- Have a saved query in Access which links to this parametric stored procedure with a fixed parameter value given => check! (
CALL myProcName('myParameterValue'), I can call that from VBA just as I do the unparametric query) - Have the ability to specify
'myParameterValue'everytime I execute the query => not check
I need to somehow specify a parameter placeholder in the SQL definition of the saved query and set that parameter in VBA. For the VBA part I have and idea I would find rather elegant:
Private Sub ParametricQuery()
Dim QDef As QueryDef
Set QDef = CurrentDb.QueryDefs("myProcName")
QDef.Parameters(*insert parameter name here*) = parameter value
Dim Result As Recordset
Set Result = QDef.OpenRecordset
Do While Not Result.EOF
MsgBox Result.Fields(1) 'Display a field from the results
Loop
End Sub
But how would I build my SQL definition?
PARAMETERS in_param TEXT;
CALL myProcName(in_param);
does NOT work. If I try
Set QDef = CurrentDb.QueryDefs("myProcName")
MsgBox QDef.Parameters.Count
I get a Messagebox telling me there is a total of 0 parameters in my query definition, so that doesn't work.
What I have found online is a lot of people building the actual SQL in VBA via string operations. That makes me shudder for so many reasons (security, maintainability and elegance among them). I firmly believe that there is a better way, hopefully along the lines I have sketched above. The only problem is: How to do it?
CREATE PROCEDUREline in MySQL (no need for whole proc)? The named param and its type is important to note.SELECT * FROM table WHERE field=param