Solved

MS Access Switch() or alternative

Posted on 2010-11-29
13
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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