Use a between statement embedded in an iif statement

klinsman08

New Member
Joined
Sep 9, 2013
Messages
2
I have 3 radio buttons belonging to a frame on a query form. Based on which radio button is selected, I want to change the query criteria.

The radio buttons have to do with which year to search for in a table. The first radio button is "Any," so all records in the table should be returned. The second radio button is "A single year," and if the user selects that radio button and enters a specific year in the corresponding box, all records containing that year should be returned. The third radio button (and the one I'm having trouble with) is "A range of years." In this one, a user enters 2 years, and the query should return all records within those two years. I have been trying to use a BETWEEN statement for this; it works by itself, but when I embed it in an iif statement (ie if the 1st radio button is selected do X, if the 2nd radio button is selected to Y, and if the 3rd radio button is selected do Z), it won't return any results.

The SQL code:

SELECT tblMaster.*</SPAN></SPAN>
FROM tblMaster</SPAN></SPAN>
WHERE (((tblMaster.Yr)=</SPAN></SPAN>

IIf(</SPAN></SPAN>
[Forms]![frmQueryMatchingJobs]![Frame48]=1,</SPAN></SPAN>
[tblMaster]![Yr],</SPAN></SPAN>

IIf([Forms]![frmQueryMatchingJobs]![Frame48]=2,</SPAN></SPAN>
[Forms]![frmQueryMatchingJobs]![txtYearFixed],</SPAN></SPAN>

IIf([Forms]![frmQueryMatchingJobs]![Frame48]=3,</SPAN></SPAN>
[tblMaster].[Yr] Between [Forms]![frmQueryMatchingJobs]![txtYearLB] And [Forms]![frmQueryMatchingJobs]![txtYearUB],</SPAN></SPAN>
[tblMaster]![Yr]</SPAN></SPAN>



)</SPAN></SPAN>
)</SPAN></SPAN>
)</SPAN></SPAN>
)</SPAN></SPAN>
);</SPAN></SPAN>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Have you looked at using the case statement in SQL? Not 100% if this works in Access (I'm assuming you're using Access) but it's probably worth a try.

On the other hand, maybe you could just build a string based on the radio buttons rather than getting the SQL to run directly from the values and then execute that instead?
 
Upvote 0
Yes, I am using Access, however I am very new at it. I've done a lot of programming in Excel, so I know VBA but not really SQL.

This is how I tried to use a SELECT-CASE statement; it gets an error when I try to run it.
SELECT tblMaster.*
FROM tblMaster
SELECT CASE [Forms]![frmQueryMatchingJobs]![Frame48]
CASE 1
tblMaster.Yr=[tblMaster]![Yr]
END SELECT

Re your string suggestion - where would this go? SQL window, Design Mode, or behind the scenes in VBA?
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top