Solved

MS Access Switch() or alternative

Posted on 2010-11-29
13
340 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now