473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using the IIF function in MS Access

I want to use the IIF function, to check for the current day, and if the condition is true, then I would like to then run a specific select statement, if false then I would want to run a different select statement. Is this possible to do with iif and have the result returned in on form with a subreport?

IE..

Expand|Select|Wrap|Line Numbers
  1. IIF(WEEKDAY(DATE(),2),<ON TRUE RUN SELECT QUERY>, <ON FALSE RUN A DIFFERENT SELECT QUERY>)
Nov 22 '11 #1
5 2624
NeoPa
32,556 Expert Mod 16PB
OK. I guess you're trying. Let me tell you why it's hard to help with a question like this. You leave out too much important information. We have to assume too much.

Firstly, I assume you are talking about SQL code. IIf() can be used in VBA too, and your question makes almost as much sense (from some perspectives more even) if considered as a VBA question than as a SQL one.

Next, I could simply say no of course, as strictly it's not able to work that way, but if I knew the context that you were working within I may be able to suggest a more appropriate alternative for you to use. Everything I want to help you with depends on your asking the question properly in the first place though - hence when you asked it first time I deleted the question and gave you the opportunity to give it more and better consideration before posting it again.

This will have to do for now, but if you take these points on board you will certainly get much better value from this site (Bytes.com), and other forum sites like it in the future.

Actually, I can add that the IIf() function is a function. Sounds obvious, but if you remember that functions return values then you would see that there is no scope for executing the value unless Jet SQL provided a facility for calling itself. SQL is normally executed from VBA code or from within an Access object (like the record source of a form for instance). Even in MS SQL's T-SQL though, which has the EXEC command for such a purpose, it isn't called from within a query's SQL. I hope that helps you to understand why the answer is no.
Nov 22 '11 #2
patjones
931 Expert 512MB
As NeoPa says, the answer really depends upon context, and you haven't given any context. I can imagine a scenario in VBA whereby you want to pick out one SQL string in one instance, and another SQL string in another instance. Notice that I'm saying string here. When you execute SQL from within VBA, the SQL string has to be attached to a valid Access object, like QueryDef or the RowSource method of a list box or combo box control.

I can also imagine the converse situation, where you might want to write a SQL statement in Access' query design window and embed an IIf( ) within the SQL. Without knowing more about you're trying to do we can only provide general answers.

Pat
Nov 22 '11 #3
I'm sorry I'm new to this site so I'm not sure how to post questions.. Yes, I'm trying to do this in a SQL query within Access. Here is the sql view window in design view of my code.

Expand|Select|Wrap|Line Numbers
  1. SELECT IIf(Weekday(Date(),2),"SELECT Count([ASP Operations Task Request List].ID) AS Expr1
  2. FROM [ASP Operations Task Request List]
  3. WHERE ((([ASP Operations Task Request List].Status)='Completed') AND (([ASP Operations Task Request List].Modified) Between Date()-1 And Date()+1));",'') AS Expr1
  4. FROM [ASP Operations Task Request List]
  5. GROUP BY IIf(Weekday(Date(),2),"SELECT Count([ASP Operations Task Request List].ID) AS Expr1
  6. FROM [ASP Operations Task Request List]
  7. WHERE ((([ASP Operations Task Request List].Status)='Completed') AND (([ASP Operations Task Request List].Modified) Between Date()-1 And Date()+1));",'');
Nov 22 '11 #4
patjones
931 Expert 512MB
To be honest, this SQL makes no sense. It is not readable and I cannot discern what you're trying to do because I can't tell where one thing ends and another begins.

At any rate, it looks like you're trying to embed one SQL statement as a string within the containing SQL. It is possible to nest SQL queries (we call them sub-queries) - but this is not how it is accomplished.

I think what you need to do is tell us in plain English what you are trying to accomplish, and then we can help you build the SQL from that. For instance, "I want to obtain the COUNT of this field in this table when this condition is met, otherwise I want the COUNT of this other field when some other condition is met".
Nov 22 '11 #5
NeoPa
32,556 Expert Mod 16PB
I can see where you're getting confused - and why this is such a confusion for you. Let's see where we can take you.

Firstly, the Weekday() function will always (in all circumstances) return a value between 1 and 7. As such your test will always be found to be non-False. That's a side-issue though.

It seems, if I read your SQL aright (and I have to try to reverse engineer your thinking here so it's a little complicated), that you would like to show the number of records found in the table for the days from yesterday till tomorrow which have a [Status] of 'Completed' as long as today is a Monday, but otherwise show nothing at all (and at some level which is still unshared cancel the process). Would that be a correct assessment?

If so, the IIf() part, as mentioned earlier, cannot be used that way at all. It is a function and returns only values. Not anything in the way of process control.

The counting SQL can be done though :
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1
  2. FROM   [ASP Operations Task Request List]
  3. WHERE  ([Status]='Completed') 
  4.   AND  ([Modified] Between Date()-1 And Date()+1)
Nov 22 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Dicken | last post by:
Hi All, I am trying to Pass A Collection To A FUNCTION ??? (Access 2000) I have a Class called BO (for Business Objects) Within BO I have a Function called ShowCollection Public Sub...
2
by: Jim | last post by:
I am a FoxPro programmer. What software do I need to program using MS access... I have apps that are non-web standalone and some web based... Do you recommend any learning resources? ...
1
by: Patrick Blackman | last post by:
Hi I need a good example using ms access with C#. I have to develop a multi user test application using access. Need some pointer on how to handle user conflicts etc. All examples I have seen use...
0
by: ss | last post by:
i read a few posts about global function access. well i am not interested in global functions. rather, i am seeking for a way to may my call in ASPX pages but not the code behind. for example:...
5
by: jhcorey | last post by:
We're using a third-party treeview and found that the functionality we need works only when we set the web site security to allow Anonymous Access. We also need the user's network id, which we...
2
by: David Dawson | last post by:
I have no experience using MS Access to interface with MySQL. I have a tiny MySQL database that might need to have Access connect with it over the internet and grab data/update etc. Is this...
0
by: Abhi | last post by:
Hi, I have to connect to Oracle 10g Database using Data Access Application block code i am using is Connection string is contained in a file called orcle.config and it contains ...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
6
by: Mark | last post by:
Currently using MS Access 2000 and SQL Server Express. Using the current DAO OpenRecordset code listed below, however I keep getting the error message.... 3254: ODBC --Cannot lock all records ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.