473,395 Members | 1,496 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.

Too many nested IIF's

Hello Everyone,

While I could run this in SQL, I'm not entirely used to the Access way of doing things. I have a query that has far too many nested IIFs and have been trying to figure out if there is a way to code the following in VBA? Or if there is another more efficient way? Any help would be greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. Country_Assignment:
  2. IIF(TRIM(UCASE([dbo_CUSTOMER]![Postcode]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  3.     ,IIF(TRIM(UCASE( [dbo_CUSTOMER]![Postcode] ))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  4.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address1]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  5.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address2]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  6.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address3]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  7.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address4]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  8.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address5]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  9.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address6]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
  10.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address1]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  11.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address2]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  12.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address3]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  13.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address4]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  14.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address5]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  15.     ,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address6]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
  16.     ,"UNKNOWN"))))))))))))))
  17.  
Gareth
Jul 8 '11 #1
18 3152
NeoPa
32,556 Expert Mod 16PB
Gareth, what are you trying to achieve (and please don't expect me to reverse-engineer your code to work out what your question should include)?

There are various functions (Switch(); Index(); etc) that can help in some case but the details determine where and when.
Jul 8 '11 #2
Hi thanks for your response.

I have a central customer table dbo.Customer with the following fields:
Address1
Address2
Address3
Address4
Address5
Address6
Postcode

And a country table that has:
Country_code
Country_name

What I'm trying to do is check each variable on the customer table (for each row) for the Country_code and the Country_name and then if it exists, assign a variable Country with the Country_name.

So if I match on any of those fields for a country name OR a country code then I get assigned that country name.

Thanks

Gareth
Jul 8 '11 #3
Sorry just to be completely clear, I want to end up with a table that has:

Customer_Code
Country

That is populated by checking each field listed above and if there is a match then a new entry goes into the new table with Country.
Jul 8 '11 #4
ADezii
8,834 Expert 8TB
What Field in dbo.Customer would uniquely identify a Customer? I see no such Field present in the Table.
Jul 8 '11 #5
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. IIf(Country_Code IN (PostCode, Address1, Address2, etc), Country_Name, IIf(Country_Name IN (PostCode, Address1, Address2, etc), Country_Name, "Unknown")
Jul 8 '11 #6
Sorry forgot to include it in my description, the id field is Customer_Code
Jul 8 '11 #7
Rabbit,

Is it really that simple!?!? I understand the code, but how do I join my tables to run that query... There are no common fields between the two tables, so how would a join work without doing a separate query where I left join on every field separately.
Jul 8 '11 #8
Customer:
Customer_Code
Address1
Address2
Address3
Address4
Address5
Address6
Postcode

Lookup:
Country_Code
Country_Name

You can see my dilemma for any joins. I can imagine this is a commonly encountered problem where one wants to scan each address field for a country (or region) and make a match. It's doing my head in because I know there must be an efficient way to do it.
Jul 8 '11 #9
Rabbit
12,516 Expert Mod 8TB
The country information should go into a country field in a first place. The only time you should need to do this is if you're cleaning up someone else's data.

You don't have to actually join on anything. You can do a cross join, meaning you join every record in the address table to every record in the country table.

Then you just filter out the ones that don't match.
Jul 8 '11 #10
Rabbit,
That's exactly what I'm doing, cleaning up another dataset.

So if I don't specify a join is that automatically a join on everything?
Jul 8 '11 #11
Rabbit
12,516 Expert Mod 8TB
I take that back, you can join on something.
Expand|Select|Wrap|Line Numbers
  1. SELECT tblAddresses.*, Nz(CountryName, "Unknown") AS CName
  2. FROM tblAddresses AS A
  3. LEFT JOIN tblCountries AS C ON
  4.    A.Address1 = C.CountryID OR
  5.    A.Address1 = C.CountryName OR
  6.    A.Address2 = C.CountryID OR
  7.    A.Address2 = C.CountryName OR
  8.    et cetera
Jul 8 '11 #12
Rabbit
12,516 Expert Mod 8TB
Not quite, if you don't sepcify a join condition, it joins on nothing. It returns every possible permutation; it's called a cartesian product.
Jul 8 '11 #13
ADezii
8,834 Expert 8TB
@Gareth:There is a purely Code based solution if you are having difficulty dealing with the SQL but it would be a little clumsier, requires no SQL, no Joins, but would not be nearly as efficient as Rabbit's approach I would imagine.
Jul 8 '11 #14
Rabbit
12,516 Expert Mod 8TB
Or you could join on
Expand|Select|Wrap|Line Numbers
  1. (A.Address1 & A.Address2 & etc ) Like ("*" & C.CountryName & "*")
  2. OR
  3. (A.Address1 & A.Address2 & etc ) Like ("*" & C.CountryID & "*")
It's hard to say which would be more efficient without running test queries on large numbers of records.
Jul 8 '11 #15
ADezii
8,834 Expert 8TB
@Gareth: How large of a Data Set are you speaking of?
Jul 8 '11 #16
NeoPa
32,556 Expert Mod 16PB
I would suggest a solution similar to Rabbit's, but without connecting on the ID field (I think that was probably a confusion that crept in), and I would also reintroduce your Trim() and UCase() calls into the mix as they can prove very helpful in such situations (They hit performance quite fundamentally, but do a more thorough job, which seems to me to be more important in this type of situation).

You could filter the data through subqueries that return the data fixed for extraneous spaces and case of course. That would make the main SQL easier to understand and work with.
Jul 11 '11 #17
Hi Guys,

Thanks very much for your help. I think Rabbit's code has been successful! Much appreciated. I'm just running some checks to make sure it has picked up all matches.

Thanks

G
Jul 11 '11 #18
ADezii
8,834 Expert 8TB
@Gareth: Here is a purely Code-based solution, should you be interested.
Attached Files
File Type: zip Demo.zip (17.3 KB, 97 views)
Jul 11 '11 #19

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

Similar topics

1
by: Johm | last post by:
I have the following NZ function : UPDATE Products SET items = NZ(items,0) + " & Nz(Quantity, 0) This function sets the null values to zeroes for computational purposes. I want to test this ...
2
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
3
by: BerkshireGuy | last post by:
Hello all. This expression is in my query and it works well. However, if both continues are false, I want it to say "Not Yet Issued". I've added that and getting an #ERROR. Any ideas? ...
6
by: Chris Waller | last post by:
Dear All, I am experiencing a problem with an IIF statement in Access. As I have never created one of these before I am having real problems getting my head around them. What I am trying to do is...
6
by: Bob | last post by:
I'm not sure why this doesn't work... EACRank: IIf(>20,"1",IIf(<14.99,"3",IIf((>14.99) And (<19.99),"2"))) The first condition returns a 1 as desired. The second condition returns a 3 The...
3
newnewbie
by: newnewbie | last post by:
Have a nested IIF statement that works but returns wrong values....Here's the syntax: Expr3: IIf(=2 Or 3,'valid',IIf(=4,'invalid','unknown')) Needed: if validity is 2 or 3 = Valid, if...
5
patjones
by: patjones | last post by:
Hi: I have a table in my Access database, "tblWC", which contains a field called "fldTrackingStatus". Furthermore, I have a report "rptTrackingReport" which is based on tblWC (using an SQL query...
5
by: ajak | last post by:
Hi Brand new to Access 2003 and stumbling at the first hurdle. I want a data base which will inform the user when a new task needs to be carried out. I have a column headed "Last Use" (date...
5
by: Merdina | last post by:
In the interest of full disclosure, I must admit that this is the first time I have ever tried using Access, let alone worked with VB. I came up with an idea for an access database that will help me...
2
by: klbrownie | last post by:
I have a database where I have two date fields, Procedure Due Date and Procedure Performed Date. I want to update a checkbox called Procedure over 30 days based on two considerations: 1 - Whenever...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.