Is it possible to pass parameters programmatically in a Microsoft Access update query? Is it possible to pass parameters programmatically in a Microsoft Access update query? vba vba

Is it possible to pass parameters programmatically in a Microsoft Access update query?


I just tested this and it works in Access 2010.

Say you have a SELECT query with parameters:

PARAMETERS startID Long, endID Long;SELECT Members.*FROM MembersWHERE (((Members.memberID) Between [startID] And [endID]));

You run that query interactively and it prompts you for [startID] and [endID]. That works, so you save that query as [MemberSubset].

Now you create an UPDATE query based on that query:

UPDATE Members SET Members.age = [age]+1WHERE (((Members.memberID) In (SELECT memberID FROM [MemberSubset])));

You run that query interactively and again you are prompted for [startID] and [endID] and it works well, so you save it as [MemberSubsetUpdate].

You can run [MemberSubsetUpdate] from VBA code by specifying [startID] and [endID] values as parameters to [MemberSubsetUpdate], even though they are actually parameters of [MemberSubset]. Those parameter values "trickle down" to where they are needed, and the query does work without human intervention:

Sub paramTest()    Dim qdf As DAO.QueryDef    Set qdf = CurrentDb.QueryDefs("MemberSubsetUpdate")    qdf!startID = 1  ' specify    qdf!endID = 2    '     parameters    qdf.Execute    Set qdf = NothingEnd Sub


Try using the QueryDefs. Create the query with parameters. Then use something like this:

Dim dbs As DAO.DatabaseDim qdf As DAO.QueryDefSet dbs = CurrentDbSet qdf = dbs.QueryDefs("Your Query Name")qdf.Parameters("Parameter 1").Value = "Parameter Value"qdf.Parameters("Parameter 2").Value = "Parameter Value"qdf.Executeqdf.CloseSet qdf = NothingSet dbs = Nothing


You can also use TempVars - note '!' syntax is essentialYou can also use TempVars - note '!' syntax is essential