How to add an additional column in query?

henrypotter

Registered User.
Local time
Today, 05:31
Joined
Mar 2, 2009
Messages
22
Hi All,

Could anyone tell me how to create an additional column in Access based on certain condition such as if sex = male, than cost + 500?

How could I write that in my VBA? Thanks!
 
What are the exact field names you will be using and what will the logic be?
 
Hi Ken,

The field name is Sex which contains Male and Female. Another field name is Cost. How do I add an additional field called cost 2 with the condition that if sex = male, cost2 = cost + 500.

should I do this in VBA? or should I do this in query or table?

Thanks!
 
In the last blank column do something like:

Cost 2: iif([sex] = 'Male', [cost] + 500, 0)

If you want it be be cost if not male then:

Cost 2: iif([sex] = 'Male', [cost] + 500, [cost])
 
Thanks Ken.

Where should I put these logics? in VBA or in query? or else where?
 
In your query on the top row of the first blank column
 
Thanks! I tried to edit that statement in SQL view but it says "You tried to execute a query that does not include the specified expression 'iif (....) as part of an aggregate function.

What does that mean?
 
Why did you not use the query builder grid tool?
 
Let me see if I can whipp you up a sample db... Give me a minute...
 
Ken,

It's now working, however, I have some records that does not input MALE or FEMALE, I want to add 500 Cost to it too. What's the word for nothing in the query code?

Thanks,
 
You should be able to change:

NewCost: IIf([sex]="male",[cost]+500,[cost])

To:

NewCost: IIf([sex] ="female",[cost],[cost]+500)

???
 
Unfortunately, I cannot open your file since I am still using Access 2000.

If [sex] is empty, I want to change the cost to 0. So there are 3 conditionns. Should I write?

NewCost: IIf([sex] ="Male",[cost],iif([sex]="",0,[cost]+500))

would access be able to recognize "" as empty?
 
If [sex] is empty, I want to change the cost to 0. So there are 3 conditionns. Should I write?

NewCost: IIf([sex] ="Male",[cost],iif([sex]="",0,[cost]+500))

would access be able to recognize "" as empty?
 
The way I read this there is one condition, if female then it should equal cost, otherwise it should equal cost + 500.
 
Sorry this is an extra question. if I want to put an extra conditional to eliminate emply Sex entry. What should I do?
 

Users who are viewing this thread

Back
Top Bottom