enter values automatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to enter a value in a field which depends on a value in another I was
trying to use the Switch function on the before update event on the first
field to put values in the second field depending on what was entered in the
first field but it wont work. Switch (Me.fault_code = 100, Me.Call_Code.Value
= "CM", Me.fault_code = 101, Me.Call_Code.Value = "ST"). I am sure this is a
stupid question but its' got me confused.
 
That should be:

Me.Call_Code = Switch (Me.fault_code = 100, "CM", Me.fault_code = 101, "ST")

However, are you sure you need this? If the value of Call_Code is always
based on the value of Fault_Code, why not have a table that stores the
mapping, and join that table to your other table?
 
I want to enter a value in a field which depends on a value in another I was
trying to use the Switch function on the before update event on the first
field to put values in the second field depending on what was entered in the
first field but it wont work. Switch (Me.fault_code = 100, Me.Call_Code.Value
= "CM", Me.fault_code = 101, Me.Call_Code.Value = "ST"). I am sure this is a
stupid question but its' got me confused.

The arguments of Switch() are the VALUES that the function return. You
can't put an operation such as Me.Call_Code.Value = "ST" as an
argument of the function.

IF you want to do this AT ALL - and see below, I think you're making a
mistake by doing so - then use

Me.Call_Code = Switch([fault_code] = 100, "CM", [fault_code] = 101,
"ST")

My concern is that if Call_Code's value can always be derived from
fault_code in this way, then the Call_Code field simply SHOULD NOT
EXIST in your table! You can instead calculate it, or look it up in a
translation table using a Query, whenever it's needed.

Storing derived data such as this in your table accomplishes
three things: it wastes disk space; it wastes time (almost
any calculation will be MUCH faster than a disk fetch); and
most importantly, it risks data corruption. If one of the
underlying fields is subsequently edited, you will have data
in your table WHICH IS WRONG, and no automatic way to detect
that fact.

Just redo the calculation whenever you need it, either as a
calculated field in a Query or just as you're now doing it -
in the control source of a Form or a Report textbox.

John W. Vinson[MVP]
 
Thanks very much of course you're correct I should join the tables, somtimes
I can't see the wood for the trees. Actually I may need the switch because
the call code was my old system and some call codes will be the same for more
than one fault code I was using two of them as an example to get the code to
work.
Once again many thanks
 
Back
Top