How to store a parameter valueUpload ImagesLink John Hirschy 15 months ago
I am writing code where I use a parameter value in a query. I would like to store that parameter value for later use in my coding. How do I store the parameter value?
Scott Axton 15 months ago
Sorry to be a bit vague, but the answer to your question is "it depends".
The method you use kind of depends on how long you want to save that value for. Just for that procedure, across multiple procedures, from session to session?
There are Variables , TempVar (not covered well here yet but we're nagging Richard), Global Variables. See: Scope
If you need the value to survive between sessions you could write it to a settings table and read it back in again in the next session or when the need for that value arrises.
If you can explain a bit about what your needs are we can help you further.
Kevin Yip 15 months ago
You have to use VBA to store the parameter in a variable. To use the parameter again for the query, you need to run the query with VBA as well. The sample code shows how to run such a query. The picture below shows how the query looks in query design. The parameter is called "Country name contains", and note how it is used in VBA.
Sub RunQueryWithParameter()
Dim q As QueryDef, r As Recordset
Set q = CurrentDB.QueryDefs("Query: Countries")
q.Parameters("Country name contains") = "united"
Set r = q.OpenRecordset
r.MoveFirst
Do While Not r.EOF
MsgBox r!country_name
r.MoveNext
Loop
r.Close
q.Close
End Sub
Kevin Yip 15 months ago
Kevin Yip 15 months ago
The line q.Parameters("Country name contains") = "united" serves the same purpose as typing "united" as the parameter when you run the query manually (see pic below).