Solved

MS Access Switch() or alternative

Posted on 2010-11-29
13
350 Views
Last Modified: 2014-06-16
For example, I have records I need to tie to data in a crosstab query.  In the query there is a conversion table that converts date to "OctRate".  I'd like to use Switch("OctRate", [OctRate]).  But that doesn't want to work.  

Any suggestions to return the value in a field in an unrelated table where the query refers to the column name?  Thank you.
0
Comment
Question by:ReneeM787
13 Comments
 
LVL 75
ID: 34233502
Not quite following:

Swithc () works like this:

Switch (<If Condition 1 = True>, <Result 1> , <If Condition 2 = True>, <Result 2> , <If Condition 3 = True>, <Result 3>)

mx
0
 
LVL 18

Expert Comment

by:lludden
ID: 34233694
In the query, you can just do a subquery (SELECT OctRate FROM tblOctRate WHERE DatetoConvert = DateToCheck)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34234020
Can you give us the range of dates and dates and the range of rates - I like mx am having difficulty 'visualizing the problem.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:ReneeM787
ID: 34234474
More info to help clarify the issue.

I am receiving data that is formatted like it came from a Crosstab query.  There are multiple fields for each month.  Using Rate as the example field: OctRate, NovRate, DecRate, etc.  I am trying to figure out how to get that data into a query for a given reporting month.  tblMonthConv converts 10/1/2010 0:00 to a text value: OctRate.  I'm trying to figure out the best way to make a calculated field pick up the Rate for each month without having to create an Iif statement with 11 conditions.

I hope that helps.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34234665
is this any help:

fldDT = #2010-11-29 17:25:22#
? MonthName(Month(fldDT),True) & "Rate"
NovRate
0
 

Author Comment

by:ReneeM787
ID: 34241516
GRayL, I tried your formula as the field in the design grid and Access wouldn't accept it.  Although, if it had, my understanding is that I'd still need a condition to test for each month.  So, I don't think it would save me anything.  Let me know if I'm misunderstanding your intention.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34242817
You're right.  You need something like the following for Access:

SELECT fldVal, Switch(Month(fldDate)=1, JanRate, Month(fldDate)=2, FebRate, Month(fldDate)=3, MarRate, Month(fldDate)=4, AprRate, Month(fldDate)=5, MayRate, Month(fldDate)=6, JunRate, Month(fldDate)=7, JulRate, Month(fldDate)=8, AugRate, Month(fldDate)=9, SepRate, Month(fldDate)=10, OctRate, Month(fldDate)=11, NovRate, Month(fldDate)=12, DecRate) * fldVal as Product from myTable
0
 

Accepted Solution

by:
ReneeM787 earned 0 total points
ID: 34248494
GrayL, I was able to make an iif statment do the job.  I'm posting the SQL below which will make it easier to see what my goal is.  This is long and messy, but it works.  I'm going to leave this open for a couple of days.  If nothing simpler is posted that works, I'll accept my own solution to close the question.

I was hoping SWITCH (or alternative) would let me specify what field would be used As CurRate.   Thanks!

SELECT DISTINCT qrya13_M_SingleParticipation.YTDYear, qrya13_M_SingleParticipation.Measure, qrya13_M_SingleParticipation.wda_no, FormatPercent(IIf(CStr([service_mth])="10/1/2010",[qrya13SinglePartBCYCalcBasis]![OctRate],IIf(CStr([service_mth])="11/1/2010",[qrya13SinglePartBCYCalcBasis]![NovRate],IIf(CStr([service_mth])="12/1/2010",[qrya13SinglePartBCYCalcBasis]![DecRate],IIf(CStr([service_mth])="1/1/2011",[qrya13SinglePartBCYCalcBasis]![JanRate],IIf(CStr([service_mth])="2/1/2011",[qrya13SinglePartBCYCalcBasis]![FebRate],IIf(CStr([service_mth])="3/1/2011",[qrya13SinglePartBCYCalcBasis]![MarRate],IIf(CStr([service_mth])="4/1/2011",[qrya13SinglePartBCYCalcBasis]![AprRate],IIf(CStr([service_mth])="5/1/2011",[qrya13SinglePartBCYCalcBasis]![MayRate],IIf(CStr([service_mth])="6/1/2011",[qrya13SinglePartBCYCalcBasis]![JunRate],IIf(CStr([service_mth])="7/1/2011",[qrya13SinglePartBCYCalcBasis]![JulRate],IIf(CStr([service_mth])="8/1/2011",[qrya13SinglePartBCYCalcBasis]![AugRate],[qrya13SinglePartBCYCalcBasis]![SepRate]))))))))))),2) AS CurRate, FormatNumber(IIf(CStr([service_mth])="10/1/2010",[qrya13SinglePartBCYCalcBasis]![OctNum],IIf(CStr([service_mth])="11/1/2010",[qrya13SinglePartBCYCalcBasis]![NovNum],IIf(CStr([service_mth])="12/1/2010",[qrya13SinglePartBCYCalcBasis]![DecNum],IIf(CStr([service_mth])="1/1/2011",[qrya13SinglePartBCYCalcBasis]![JanNum],IIf(CStr([service_mth])="2/1/2011",[qrya13SinglePartBCYCalcBasis]![FebNum],IIf(CStr([service_mth])="3/1/2011",[qrya13SinglePartBCYCalcBasis]![MarNum],IIf(CStr([service_mth])="4/1/2011",[qrya13SinglePartBCYCalcBasis]![AprNum],IIf(CStr([service_mth])="5/1/2011",[qrya13SinglePartBCYCalcBasis]![MayNum],IIf(CStr([service_mth])="6/1/2011",[qrya13SinglePartBCYCalcBasis]![JunNum],IIf(CStr([service_mth])="7/1/2011",[qrya13SinglePartBCYCalcBasis]![JulNum],IIf(CStr([service_mth])="8/1/2011",[qrya13SinglePartBCYCalcBasis]![AugNum],[qrya13SinglePartBCYCalcBasis]![SepNum]))))))))))),2) AS CurNum, FormatNumber(IIf(CStr([service_mth])="10/1/2010",[qrya13SinglePartBCYCalcBasis]![OctDen],IIf(CStr([service_mth])="11/1/2010",[qrya13SinglePartBCYCalcBasis]![NovDen],IIf(CStr([service_mth])="12/1/2010",[qrya13SinglePartBCYCalcBasis]![DecDen],IIf(CStr([service_mth])="1/1/2011",[qrya13SinglePartBCYCalcBasis]![JanDen],IIf(CStr([service_mth])="2/1/2011",[qrya13SinglePartBCYCalcBasis]![FebDen],IIf(CStr([service_mth])="3/1/2011",[qrya13SinglePartBCYCalcBasis]![MarDen],IIf(CStr([service_mth])="4/1/2011",[qrya13SinglePartBCYCalcBasis]![AprDen],IIf(CStr([service_mth])="5/1/2011",[qrya13SinglePartBCYCalcBasis]![MayDen],IIf(CStr([service_mth])="6/1/2011",[qrya13SinglePartBCYCalcBasis]![JunDen],IIf(CStr([service_mth])="7/1/2011",[qrya13SinglePartBCYCalcBasis]![JulDen],IIf(CStr([service_mth])="8/1/2011",[qrya13SinglePartBCYCalcBasis]![AugDen],[qrya13SinglePartBCYCalcBasis]![SepDen]))))))))))),2) AS CurDen
FROM qrya13_M_SingleParticipation, qrya13SinglePartBCYCalcBasis;
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34253231
Solution coming tomorrow, hang in there.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34258737
Which query contains the field service_mth ?
0
 
LVL 44

Assisted Solution

by:GRayL
GRayL earned 250 total points
ID: 34259151
Assuming that field is in the second query try this:

SELECT DISTINCT a.YTDYear, a.Measure, a, FormatPercent(
Switch(Month(b.Service_Mth)=1,JanRate, Month(b.Service_Mth)=2,FebRate, Month(b.Service_Mth)=3,MarRate, Month(b.Service_Mth)=4,AprRate, Month(b.Service_Mth)=5,MayRate, Month(b.Service_Mth)=6,JunRate, Month(b.Service_Mth)=7,JulRate, Month(b.Service_Mth)=8,AugRate, Month(b.Service_Mth)=9,SepRate, Month(b.Service_Mth)=10,OctRate, Month(b.Service_Mth)=11,NovRate, Month(b.Service_Mth)=12,DecRate),2) AS CurRate,
FormatNumber(
Switch(Month(b.Service_Mth)=1,JanNum, Month(b.Service_Mth)=2,FebNum, Month(b.Service_Mth)=3,MarNum, Month(b.Service_Mth)=4,AprNum, Month(b.Service_Mth)=5,MayNum, Month(b.Service_Mth)=6,JunNum, Month(b.Service_Mth)=7,JulNum, Month(b.Service_Mth)=8,AugNum, Month(b.Service_Mth)=9,SepNum, Month(b.Service_Mth)=10,OctNum, Month(b.Service_Mth)=11,NovNum, Month(b.Service_Mth)=12,DecNum),2) AS CurNum,
FormatNumber(
Switch(Month(b.Service_Mth)=1,JanDen, Month(b.Service_Mth)=2,FebDen, Month(b.Service_Mth)=3,MarDen, Month(b.Service_Mth)=4,AprDen, Month(b.Service_Mth)=5,MayDen, Month(b.Service_Mth)=6,JunDen, Month(b.Service_Mth)=7,JulDen, Month(b.Service_Mth)=8,AugDen, Month(b.Service_Mth)=9,SepDen, Month(b.Service_Mth)=10,OctDen, Month(b.Service_Mth)=11,NovDen, Month(b.Service_Mth)=12,DecDen),2) AS CurDen  
FROM qrya13_M_SingleParticipation a, qrya13SinglePartBCYCalcBasis b;
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question