I have n-columns that may contain unique or duplicated values meaning each column may have different values or the same ones.
What I want to achieve is an MS Access script that can analyze n-columns that could do the following:
If the columns if only one has a value and the rest are blank then the final value will be the only one seen (Note: this one is already accomplished).
+-----+----+----+-----+ |Var1 |Var2|Var3|Final| +-----+----+----+-----+ |1 | | |1 | +-----+----+----+-----+ | | |3 |3 | +-----+----+----+-----+ | |yes | |yes | +-----+----+----+-----+
If there are more than one different values then ask the user to choose between these values (Update: precedence of variables instead of user selection ie.
Var1
>Var2
> ... >VarN
).+----+----+----+-----+ |Var1|Var2|Var3|Final| +----+----+----+-----+ |1 |3 | |? | +----+----+----+-----+ |ok | |3 |? | +----+----+----+-----+ | |yes |5 |? | +----+----+----+-----+
If there are more than one same values then proceed to use that as the final value.
+----+----+----+-----+ |Var1|Var2|Var3|Final| +----+----+----+-----+ |1 |1 | |1 | +----+----+----+-----+ |ok | |ok |ok | +----+----+----+-----+ | |yes |yes |yes | +----+----+----+-----+
Solve all three situations, the code below takes care of precedence, empty and possible null values in the content.
INTO temp
will save the query result into a table namedtemp
.Transfer the records into the source table.
The question "Combine values from n-fields with precedence" helped!
On reviewing the answers to your previous questions on this topic they seem to rely pretty heavily on
IIf(Var1 <> Var2, ...
constructs. If you really want an n-column solution for n of any non-trivial size the first thing you need to do is convert your columns into rows. For sample data [Table1]:Create a saved query in Access named [Unpivoted]:
That query returns the following, and will be the basis from which we work:
Our first step will be to pull out the values where RowID has just one distinct non-Null value. We'll save this query as [NoSelectionRequired]
That returns
So we can cover Cases 1 and 3 with the following query (see also Edit1, below)
After we run that query [Table1] now looks like this:
Now... for Case 2:
You need to provide a mechanism for the users to pick the values they want. One way you could do that would be to use a [Selections] table...
...that you could populate with a statement like this
so your [Selections] table contains
Slap that into a continuous form with checkboxes for the [Selected] field and a bit of logic behind it to only allow one selection per RowID, then once the user has made their choices:
then you can update the remaining rows in [Table1] (see also Edit1, below)
and there you have it:
Edit1 - re: comment
For larger datasets the two queries that write the results to the [Final] column may take a long time to run because they are not terribly efficient. In that case the solution would be to dump the results to a temporary table and then use that to update the main table.
For the first update (Cases 1 and 3), instead of doing
we would do...
...followed by...
For the second update (Case 2), instead of doing
we would do...
...followed by...