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! -
Country_Assignment:
-
IIF(TRIM(UCASE([dbo_CUSTOMER]![Postcode]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE( [dbo_CUSTOMER]![Postcode] ))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address1]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address2]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address3]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address4]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address5]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address6]))=TRIM(UCASE([ISO_Country_LOOKUP]![Country_Code])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address1]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address2]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address3]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address4]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address5]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,IIF(TRIM(UCASE([dbo_CUSTOMER]![Address6]))=TRIM(UCASE([ISO_Country_LOOKUP_1]![Country_name])), [ISO_Country_LOOKUP_1]![Country_name]
-
,"UNKNOWN"))))))))))))))
-
Gareth
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.
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
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.
What Field in dbo.Customer would uniquely identify a Customer? I see no such Field present in the Table.
- IIf(Country_Code IN (PostCode, Address1, Address2, etc), Country_Name, IIf(Country_Name IN (PostCode, Address1, Address2, etc), Country_Name, "Unknown")
Sorry forgot to include it in my description, the id field is Customer_Code
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.
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.
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.
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?
I take that back, you can join on something. - SELECT tblAddresses.*, Nz(CountryName, "Unknown") AS CName
-
FROM tblAddresses AS A
-
LEFT JOIN tblCountries AS C ON
-
A.Address1 = C.CountryID OR
-
A.Address1 = C.CountryName OR
-
A.Address2 = C.CountryID OR
-
A.Address2 = C.CountryName OR
-
et cetera
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.
@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.
Or you could join on - (A.Address1 & A.Address2 & etc ) Like ("*" & C.CountryName & "*")
-
OR
-
(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.
@Gareth: How large of a Data Set are you speaking of?
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.
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
@Gareth: Here is a purely Code-based solution, should you be interested.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ...
|
by: Jim Irvine |
last post by:
Does anybody know the limit of nested iif statements you can use in Access?
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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...
| |