473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Case or IIF?

17
I want to show certain text depending on the date/date range.

Example if date is between 1/1/08 and 3/31/08, text on a report would reflect as "First Quarter"
4/1/08 - 6/30/08 would show text Second Quarter.

I tried using >=, i tried building into the query as a temp field, but it does not seem to work like i thought it would.

'If [quarter] >= "01/01/2008" or =< "03/31/2008" Then
'Me.[Quarter] = "First Quarter"

I wasn't sure if to use the Case function or an IIF statement...

suggestions.
Feb 19 '08 #1
5 2001
Scott Price
1,384 Expert 1GB
Be aware that the DatePart() function includes the ability to tell which quarter the passed in date belongs to...

For example:

Expand|Select|Wrap|Line Numbers
  1. DatePart("q", Now())
will return the current quarter of the current year. You can replace Now() with any valid date format, including a date string (enclosed in # marks, i.e. #1/1/2008#).

You can then use this numbered value to determine what text you wish to show on the report... For example, in the OnOpen event of your report, you can place code something like this:

Expand|Select|Wrap|Line Numbers
  1. If DatePart("q", [PassedInDate]) = 1 Then
  2.      Me.Label1.Caption = "First Quarter Results"
  3. ElseIf DatePart("q", [PassedInDate]) = 2 Then
  4.      Me.Label1.Caption = "Second Quarter Results"
  5. 'etc etc etc
  6. End If
Kind Regards,
Scott
Feb 19 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
... I tried using >=, i tried building into the query as a temp field, but it does not seem to work like i thought it would.
'If [quarter] >= "01/01/2008" or =< "03/31/2008" Then
'Me.[Quarter] = "First Quarter"...
Hi. A number of contributors find dates a bit difficult to manipulate, for similar reasons. Your query is not actually comparing dates to dates, but text values which look like dates to dates. To get you going a bit change "01/01/2008" and "03/31/2008" in your query to #01/01/2008# and #03/31/2008#. The hash marks are Access's syntax for defining within queries and in direct passing of values to parameters that the characters that follow to the closing hash are dates.

I think this will not take you very far, and you need to give some further thought to what you are wishing to achieve. If you wish to filter a report you will need changeable values to compare the rows against, and these will have to be provided by the user using a text box or combo box to enter or select date values. Comparing text box values to dates requires conversion to an actual date using CDate(), say.

If you want to select reports for given quarters of the year you can calculate the report quarter and year using a summary query on the dates in your table. Assuming that the underlying date values are really dates, and not text strings, you can use Year([name of date field]) to return the current year, and Month([name of month field]) to return the current month. The quarter can be calculated from the month as
Int(Month([name of month field]) / 4) + 1.

The calculated Year and Quarter fields can be used in a totals query to be the source for a selection of the specific periods to be reported. I suspect this would be a fair amount of work for you to achieve at this stage, though!

Hope your developments progress

-Stewart
Feb 19 '08 #3
Scott Price
1,384 Expert 1GB
Hi Stewart,

Thanks for your input. I'm just wondering about calculating the quarter the way you have done it, while it's a valid way of arriving at the quarter, why not just use the DatePart("q", [Date]) method, which is easier and works just as well in queries as in VBA?

Anyway we can wait till we find which way the OP wishes to go :-)

Regards,
Scott
Feb 19 '08 #4
jaxjagfan
254 Expert 100+
I use Scott's method but try to avoid writing any VBA if possible.

Expand|Select|Wrap|Line Numbers
  1. Select "Quarter " & DatePart("q", tblMyData.MyDate) as Qtr, Sum (tblMyData.MyValue) as TotVal
  2. From tblMyData
  3. Group By DatePart("q", tblMyData.MyDate)
  4.  
This will change your text to "Quater 1" but no iif's or case's required. It will also group and sum the data to the Quarter level.
Feb 19 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
I use Scott's method but try to avoid writing any VBA if possible.
Thanks Scott and jaxjagfan for the tips. When I mentioned the quarter calculation (however its done - and athough I have used date functions for many years this is the first time I have come across the datepart function, which seems better to me too!) it was not for VBA coding, but for direct inclusion as a calculated field in the grouping query, just as jaxjagfan notes. I, too, try wherever possible to use queries to do the work. VBA is hidden away from view, making it more difficult for a user to understand what a query is doing if a lot of its processing is done by VBA instead of the SQL.

Looking ahead to what CKRows wants to do with the data once filtered is where I think there could be a need for some VBA, if only to apply the same filtering to the underlying report.

This is a great forum to contribute to - keep up the good work!

-Stewart
Feb 20 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: Elliot Temple | last post by:
Hi I have two questions. Could someone explain to me why Python is case sensitive? I find that annoying. Also, why aren't there multiline comments? Would adding them cause a problem of some...
17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.