help needed using the switch function in a query

martinr

Registered User.
Local time
Tomorrow, 00:26
Joined
Nov 16, 2011
Messages
74
i am getting a syntax error when trying to run an access select query - the query works fine until i include this Switch statement;

genderStatus: Switch([gender]="M","Male",[gender]="F","Female",[gender]="X","Cross gender")

should this sql statement work?
 
lot easier to make a simple table
TblGender
genderID
genderDescription

Then link to that. Example

M Male
F Female
X Cross Gender
 
Last edited by a moderator:
you can use a table as the other contributor suggested, and use DLookup() function in your query.
Or you may expand the Switch for "unspecified" gender.
Code:
genderStatus: Switch([gender]="M","Male",[gender]="F","Female",[gender]="X","Cross gender", True, "Unspecified")
 
If you use the table suggested by MajP, you would never use DLookup() to get the value, you would use a left join which is much more efficient. In the past, I wouldn't have had a problem with using Switch for something like this where the possible value set was small and unchanging but in the world today, it is popular to think of gender as fluid and having many more options than what you are using. If you are working in an environment like that, go with the table so you can let the users add genders at will rather than requiring you to change queries, code, forms, etc.
 
I'll toss in a motivator. Make it a table like MajP suggested. If you are using a one-character code, even a short table to act as translator via a JOIN of some flavor makes sense if you ever decide you wanted to have this as an option in a combo box. Because if you go that route, the translator table could drive that control trivially as its RowSource.
 
I should add that I have an entire sub-system that manages this type of lookup. I add it to every new application I build. I'm sure I've posted it here a dozen times.
 
I should add that I have an entire sub-system that manages this type of lookup. I add it to every new application I build. I'm sure I've posted it here a dozen times.

How can I search for it?
Do you have a link to one of those posts?

thank you.
 

Users who are viewing this thread

Back
Top Bottom