View Full Version : Run a query by command button


TMcAteer
02-23-2002, 05:22 AM
HI I have a problem using the syntax in access 97, the problem is that i have three queries but i only want to run one of them. The way i want to select the query to run is by opening a form with a subform in it, the fields that i am using are in the subform but i have to open it using the main form so the right record is already selected for use on the query

I want to use the IF THEN statement to run each query. In my form i have 3 check boxes each with different names, eg check1, check2, check3. If check1 is ticked/true then i want it to run query1, but if check2 is true then i want it to run query2. How would i go about this using a command button on the subform.

I would be grateful for any help at all

Tom

Fornatian
02-23-2002, 06:07 AM
Tom,

Consider this...

1. Do you want the user to be only able to run queries 1,2 OR 3? I think so...

If so delete the checkboxes and put an option group on there instead to make the choices mutually exclusive.

Then put a single button on with the following code:

Dim strQryNme as string

strQryName = Choose(Me.OptionGroupName +1,"Query1","Query2","Query3")
'add one because the option chosen is stored as a zero based value and the choose function starts at 1.

Docmd.OpenQuery strQryNme

if it's an action query you will want to surround the above open query method with SetWarnings False prior and SetWarnings True
after. It is imperative when doing this to make sure you turn the warnings back on after or you'll never know what errors are going on.

HTH

Ian

TMcAteer
02-23-2002, 06:38 AM
Thanks a lot it works and was a great help

Cheers Tom