How can one set parameter values of an INSERT SQL query through VBA in Microsoft Access?

Romi24 picture Romi24 · Aug 4, 2012 · Viewed 25.7k times · Source

I am new to Access and I am coming from C#, SQL Server & .Net. There is a project that has come my way and I have to complete some parts.

The scenario may be described as:

  1. An Access form with a subform
  2. An Access query that is the data source of the aforementioned subform, with two parameters, displayed as: Parametername1 String(255),Parametername2 String(255).
  3. VBA code module(s)

My plan is to set the values of the above mentioned query parameters within a procedure in my VBA code module. I believe this should refresh my subform as the query is the datasource for the subform.

The problem is I don't know how to implement this plan.

I want to use a query because I don't want to mess up my VBA Code with inline SQL.

I am using Access 2010.

Answer

Ben picture Ben · Feb 18, 2013

I had exactly this question, I wanted to use the same 'stored' update query but execute it from two different forms so wanted to pass the parameter to the query at run-time. This is what I found (in another forum) that does exactly what I want:

With CurrentDb.QueryDefs("qry_YourQuery")
   .Parameters("yourParam") = yourVBAvar
   .Execute
End With