Avatar of Sivasan
Flag for United States of America asked on

How can we use two different query based on if statement in an access form record source


I have an access form where record source is a query. I like to know if there is a way, we can use if statement to choose between 2 different query say query1 and query 2 to be used on the record source.

I have another form say Form1 and a click button event which will launch this form From2.

Based on an entry on a text field on Form1, i want to launch Form2 either using one of the query on the record source.

I don't want to replicate the Form2 which uses query1 and do it as Form3 with query2 and based on condition on Form1 launch Form 2 or 3, reason this form is complex and I have multiple forms so I have to do this for all. Rather if I can control which query to use from record source will be awesome.

Wondering if there is an option.

Microsoft Access

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)

8/22/2022 - Mon
Jim Dettman (EE MVE)

<<Based on an entry on a text field on Form1, i want to launch Form2 either using one of the query on the record source. >>

  Yes, that's very doable in  many ways.   Two simpler methods:

1. Modify the SQL of the query object in form 1 before you open form 2.  Form 2 always points to the same query object and requires no additional code.


2. Use the OpenArgs argument to pass a flag to form 2 on what it should use.   In its OnOpen event, it would set its recordsource based on that flag.
    The recordsource could be set to either two different queries, or with two different SQL statements directly.


Hi Jim,
I get your idea, I pass from Form1 and use OpenArgs to get that flag in Form2
I have a small problem, Form2 has 2 forms embedded on it (form3 and 4) two tabs it's the Form3 and Form4 whose recordsource I want to control based on this form.
On the Open event of Form2, I'm not sure how I can pass this to form3 and 4, when Form 2 opens.
Because it's lot of information, I have 2 tabs on top the user clicks to see information either on Tab 1 or 2.
So this Form 3 and 4 is the one I want to display the information based on either one of the query.
Is there a way I can pass the flag to this Form 3 and 4 when Form 2 opens ?

Jim Dettman (EE MVE)

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy