What you do is write a funtion to determine what value needs to be returned
based on the value or values you pass to it. It needs to be a Public
Function in a standard module.
Then you put the function in your query where you need to return the value.
For example, lets say you want to update the field in your table with the
values based on your original rules, you would put it in the Update To row of
your query in the column for that field.
The function would look something like this:
Public Function GetLevel(lngSerialNmber as Long) As String
If lngSerialNumber = 1113 Or lngSerialNumber = 1713 Then
GetLevel = "Iron"
ElseIf lngSerialNumber >= 1200 And lngSerialNumber <= 1300 Then
GetLevel = "Copper"
ElseIf lngSerialNumber >= 1000 And lngSerialNumber <= 2000 Then
GetLevel = "Gold
Else
GetLevel = Null 'Or whatever you want to return if it is
anything else
End If
End Function
Then in the query, you pass it the value of the field in the record to
evaluate
GetLevel([SerialNumber])
A couple of notes
To get a query to execute for every row in a query, you have to pass it a
value. It does not have to be a field name or have any meaning. I have had
a case where I needed the function to process for each record, but not use
anything from the record, so I did DoMyFunction(""). The function doesn't
even have to look at what is passed.
Next, and this may be important. I just assumed that SerialNumber is a Long
Integer. You may have to type it differently, depending on the data type of
the field. If it is possible there could be records where the SerialNumbder
is Null, then you would need to change the data type in the function to
Variant or it will cause an error.
EmOXon said:
Currently I am using nested IIf method, trouble is - it gets too
complicated, if I have many areas (like a large box and many small
boxes inside). Also
Can I hear more about writing a function, I think that is what I am
looking for. A simple example would be most appreciated.
Thanks
You could write a function and call it from the query, or, a nested IIf()
function could do it.
IIf([SerialNumber] between 1000 And 2000, IIf([SerialNumber] In(1113, 1713),
"Iron", IIf([SerialNumber] Between 1200 And 1300, "Copper","Gold")), NUll)
In the Query Builder, you would put this in the Update To Column of the
field you want to put these values in.
Be aware the above is untested "air code" and the syntax may or may not be
correct, but it should give you the basic idea (or a bad headache)
:
Hi everyone,
I have a table with a field that I need to update based on the certain
conditions. For example, if Serial number are in range of 1000 to 2000
it would filled field with value "Gold", except for for specific values
1113 and 1713 I need it to be marked as "Iron" and range of 1200 to
1300 marked as "Copper". I would like to do it in the single run -
rather than - in this case - run 3 update queries. I suppose I should
be able to do in VB module?
Thanks in advance
EM