473,395 Members | 1,915 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,395 software developers and data experts.

Combining IsNull and IIf in an Expression

Good afternoon folks, I'm a total novice at this game.

I'm a student at an University in DC and is taking a basic course in Access and database. I need some help writing a nesting expression using Isnull and iff functions.

I have a field titled "Revision_box" that would display either 0,1,2, or 3 depending if certain fields are empty or has been inputted in.

So,

If Round 1 field is empty then Rev box would be "0" but if inputted then 1.
If Round 2 field is inputted then Rev box would be "2".
If Round 3 field is inputted then Rev box would be "3".

Does that make sense? I wrote a code for it but it doenst work...

Expand|Select|Wrap|Line Numbers
  1. Revision_box: IIF(IsNull([Round_1]), "0", IsNull([Round_2]), "0", "2", IsNull([Round_3]), "0", "3")
Oct 28 '09 #1

✓ answered by ajalwaysus

@DBlearner
Your issue is still the same as I pointed out before, you are assuming you can just use one IIF function, and just divide it with "OR"s. You cannot do that, for every possible "OR" you need to make another IIF.

As below:
Expand|Select|Wrap|Line Numbers
  1. CAR_Revision_1: IIf(Not IsNull([CAR EVMC Response Comment (1)]),"1", IIF(Not IsNull([CAR CMO Response Comment (1)]), "2", IIF(Not IsNull([CAR CMO Response Comment (2)]), "3", "0")))
I have not tested this, but this is the general idea.

-AJ

8 29967
ajalwaysus
266 Expert 100+
I don't believe we are allowed to give you an answer to a class question, but I believe I can give you some advice, I see what is wrong.

You need to learn more about the IIF functon. You are assuming that you can you can make multiple evaluations in a single IIF statement, that is not correct. Each IIF statement expects 1 condition and 1 if false and 1 if true statement, like:
Expand|Select|Wrap|Line Numbers
  1. iif (condition, value_if_true, value_if_false )
If you want to do more, you need to embed more IIF inside each other like below:
Expand|Select|Wrap|Line Numbers
  1. iif (condition, value_if_true, iif (condition, value_if_true, iif (condition, value_if_true, iif (condition, value_if_true, value_if_false ))))
Hope this points you in the right direction.

-AJ
Oct 28 '09 #2
Thank you for the quick response.

This is not for grade but my personal attempt to understand expressions and how to properly nest functions. Anyway, I understand your code and it makes sense. But there's a problem... I need to use Isnull function at the beginning but cant find another function that is the opposite of Isnull for the subsequent nesting in the expression.

I would use IsNull at the beginning to tell the revision box to put in "0" when the field is null. However, how can I make the expression react accordingly when subsequent fields has been inputted?

God I love this!

Mike
Oct 28 '09 #3
NeoPa
32,556 Expert Mod 16PB
You don't need to use the IsNull() function in a query. X Is Null works anywhere SQL code is used (in queries specifically). Either should work for you.

Checking for Null is a little different from ordinary checks as the only way to check for Nulls in VBA is to use the IsNull() function. In SQL X Is Null also works of course. Checking for something not being Null is the same except you use Not with it (X Is Not Null).

IIf() is pretty powerful and can be used extensively. However, other useful functions include Switch() & Choose(). You will find out more about these in the Help system. If you're learning Access you want to make a friend of the Help system. It exists for Access itself, as well as VBA from the IDE (VBA Editor) window. You'd be better served by Switch() in this instance.
Oct 29 '09 #4
Neo,

I didnt know about Switch, etc and I just learned several new functions, thx. I dont see how Switch() would be a good solution...

I ended up finding out about If-Then-Else statement and it's perfect for what I wanted to do.

I wrote this VBA code since this one can only work in VBA and not in Query. However, nothing happened and I am scratching my head.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CAR_Revision_1()
  2.  
  3. If [CAR EVMC Response Comment (1)] = IsNull Then [CAR_Revision_1] = "3"
  4. ElseIf [CAR CMO Response Comment (1)] = IsNull Then [CAR_Revision_1] = "2"
  5. ElseIf [CAR CMO Response Comment (2)] = IsNull Then [CAR_Revision_1] = "1"
  6.  
  7. Else: [CAR_Revision_1] = "0"
  8.  
  9. End If
  10.  
  11. End Sub 
Nov 2 '09 #5
I decided to do a different tack and used a different funtion for a query table. I think this may be more efficient and easier to do than doing "If Then Else" statement in VBA. However, this one seem not to be working properly because of syntax issue.

Expand|Select|Wrap|Line Numbers
  1. CAR_Revision_1: IIf(Not IsNull([CAR EVMC Response Comment (1)]),"1", or (Not IsNull([CAR CMO Response Comment (1)]), "2", or (Not IsNull([CAR CMO Response Comment (2)]), "3", "0")
Nov 2 '09 #6
ajalwaysus
266 Expert 100+
@DBlearner
Your issue is still the same as I pointed out before, you are assuming you can just use one IIF function, and just divide it with "OR"s. You cannot do that, for every possible "OR" you need to make another IIF.

As below:
Expand|Select|Wrap|Line Numbers
  1. CAR_Revision_1: IIf(Not IsNull([CAR EVMC Response Comment (1)]),"1", IIF(Not IsNull([CAR CMO Response Comment (1)]), "2", IIF(Not IsNull([CAR CMO Response Comment (2)]), "3", "0")))
I have not tested this, but this is the general idea.

-AJ
Nov 2 '09 #7
NeoPa
32,556 Expert Mod 16PB
@DBlearner
It's not actually VBA code you need here. It's SQL. Try :
Expand|Select|Wrap|Line Numbers
  1. [CAR_Revision_1]: Switch([CAR EVMC Response Comment (1)] Is Null,'3',
  2.                          [CAR CMO Response Comment (1)] Is Null,'2',
  3.                          [CAR CMO Response Comment (2)] Is Null,'1',
  4.                          True,'0')
The spacing is only necessary for display purposes here. In the query you can have it as a single line if you want.
Nov 2 '09 #8
It worked perfectly. Thank you so much for the help. I'm learning and loving it. Thanks man!
Nov 17 '09 #9

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

Similar topics

5
by: JackT | last post by:
Hi, I have the following SQL SELECT Table1.Col1, Table3.Col1 AS Expr1, COUNT(Table1.Col2) AS Col2_No, COUNT(Table1.Col3) AS Col3_No etc, FROM Table3 INNER JOIN Table2 ON...
3
by: Matik | last post by:
Hello to all, Below the sample code: declare @arg_szMsgText ntext set @arg_szMsgText = isnull(@arg_szMsgText, N'unknown message') Now the error message I get: "The assignment operator...
6
by: Eric J Owens | last post by:
TIA! I recently moved some forms from an a2k mdb file to an a2k adp. There is now an error when opening one of the forms 'the isnull function requires 2 arguments', but I only find references...
4
by: Markus | last post by:
I receive the foolwoing error in my report : "You entered an expression that has no value" In my report i am summing up the all orders with the control =Sum() This control is called Bonus. I...
5
by: M.Stanley | last post by:
Hi, I'm attempting to create a query that will combine 2 columns of numbers into one. The followng comes from 1 table with 4 fields (A,B,C,D) A B RESULT 700 000 700000 700 001 ...
1
by: et | last post by:
How do you know when to use isnull, and isdbnull and how do you use them? I have the following expression in C: DataRowView drv = dataItem as DataRowView; if (drv != null) How do I convert...
3
by: Jan Nielsen | last post by:
Hi I am working with rowfilters in dataviews. I would like to filter for empty fields (= null value in the database) I found this sentence on msdn: **************** To return only those columns...
16
by: madeleine | last post by:
Please can someone help me, I think I may go mad with this one: Do While Not IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate))) If IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate)))...
74
by: Beany | last post by:
Hi, I have got an ADD button with the following code: Private Sub addrec_Click() On Error GoTo Err_addrec_Click DoCmd.GoToRecord , , acNewRec Exit_addrec_Click:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.