Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building custom expression (ms access)

Status
Not open for further replies.

OCM

MIS
Sep 12, 2002
218
0
0
US
Hi,

I have a table with the following fields/values:

Status: open, closed, pending
ClosedReason: 1, 2. 3

If status = closed, then ClosedReason will display one of the numeric values from drop down.
However, I would like to change the query criteria and build expression so that the query returns the text description follows.
1: closed on site
2: closed off site
3: Referred

How can I accomplish the above?

TIA

Regards


OCM
 
Add the table containing 1: closed on site 2: closed off site 3: Referred to the query. If you don't have a table (maybe not a good idea) you can create an expression using the Choose() function:

Code:
ClosedReasonText: Choose([ClosedReason], "closed on site", "closed off site", "Referred")

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Hi,
Sorry for not clarifying. The table only contains numeric values (no text descriptions). Basically, I want to replace the numeric value with text e.g. if ClosedReason= 1 then display "closed on site", if ClosedReason =2, then show me "closed off site" etc.

TIA


OCM
 
That's exactly what I provided in the CODE box. You should however make this more flexible by creating a small lookup table. It would have taken less time than replying back.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Hi,
I'm relatively new and the db was design by someone else and apparently didn't incorporate the description during the design process.
What I was thinking was to create expression like IIF([ClosedReason] = 1, "Closed on site"....
but wasn't sure about the correct syntax.

TIA

OCM
 
Now you are venturing into the VBA world, which has betted help in forum707

So if you have something like this in column A and want [blue]this in column B[/blue]:
[pre]
A B
1 1[blue] closed on site[/blue]
2 3[blue] Referred[/blue]
3 2[blue] closed off site[/blue]
4 1[blue] closed on site[/blue]
[/PRE]
You could create a User Defined Function:

Code:
Public Function ClosedReason(ByRef intIn As Integer) As String
Dim strOut As String

Select Case intIn
    Case 1: strOut = "closed on site"
    Case 2: strOut = "closed off site"
    Case 3: strOut = "Referred"
End Select
ClosedReason = strOut

End Function

Now you can use your own formula in cell B1: [blue][tt]=ClosedReason(A1)[/tt][/blue]

BTW - this is just to prove the VBA route, but you should NOT keep the data in the code. You should have your data in the table in the spreadsheet.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
I agree with Andy except the "spreadsheet" part ;-)

The question should have been posted in one of the MS Access forums based on the subject line.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Thank you both for the reply post. Andy, thank you for the vba link as well.
Regards,

OCM
 
Sorry about the "spreadsheet" part. It is not Excel, it is Access.
If that's the case, the solution is even easier:

Create a table:[pre]
ClosedReason
ID Reason
1 closed on site
2 closed off site
3 Referred
[/pre]
And use it in your queries wherever you want to display this information.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top