How to write a better If Then Else

Hi all,

I want to display day number of a month, excluding the Saturday and Sundays and other non working days when some exists (like July 4th for you US people).

All I could found was a special If …/… ElseIf …/… End If block:

If ThisMonth.Day = 4 Then // Exclude July 4th // Do nothing ElseIf ThisMonth.DayOfWeek < 2 And ThisMonth.DayOfWeek < 7 Then // Exclude Sundays and Saturdays // Add all days: Monday thru Friday End If

Is there’s something less complex to do that ?

Nota: I get a Cold on last Tuesday and I am unsure that I coded that correctly even if this seems to be correct / works fine AFAIK.

Thank you for your help.

PS: I have to display some concept for some anti idiot proof entries in a running software done by benevolents persons. In the case above, I try to avoid people making entries on Saturdays, Sundays and other non working days: the day number is selected using a PopupMenu built depending on the Month / Year number.

I also implemented a PopupMenu that is populated from a list of country names to choose the name of the country and avoid typographical errors like;

USA, USoA, U.S.A., United State of America and so on. In that Case it will be U.S.A.

You can’t imagine how people can be inventive to insert errors when they work (for a fee or not). The data are stored in a SQLite data base and as we all know “France” and “france” are two different words !
And for the statistics, say the number of different countries or number of persons by country

[code]select case ThisMonth.DayOfWeek

case 1,7
if ThisMonth.Day <> 4 then DoAnythingYouWant

end select
[/code]

Your code is wrong.
It add a day only when ThisMonth.DayOfWeek=1 (Sunday)

This is what I’d write:

if ThisMonth.DayOfWeek <> 1 and ThisMonth.DayOfWeek <> 7 and ThisMonth.Day <> 4 // Add all days: Monday thru Friday End If

Hijacking the thread slightly, you’ve reminded me of a comment of mine I found earlier this week. It’s for code to query a SQLite database for records with a date ‘in this week’. I can’t easily use SQLite’s %W because we allow people to set the date on which the week starts in our application. It just boggled my mind when I re-read it… particularly ‘(app.dateStartOfWeek+5) mod 7’.

      [code] // RS day of week is 1 for Sunday thru 7 for Saturday.
      // SQLite day of week is 0 for Sunday thru 6 for Saturday, so we have to subtract 1 from the RS day of the week to get the SQLite day of the week.
      // Let's say today is Monday 20 February 2012.
      // Doing date('2012-02-20', 'weekday 1') will return 2012-02-20 as the weekday modifier advances to the next day of the week where the weekday number is N, but doesn't advance if N = today's weekday.
      // Doing date('2012-02-20', 'weekday 0', '-6 days') will give us the Monday at the start of this week, as we're advancing to next Sunday and then subtracting six days.
      // Translating this into application constants, so people can choose the day the week starts, we need to get the day of the week before today - eg if today is 2 we need 1 and if today is 1 we need 7.
      // Next, translating that into SQLite, we need to subtract 1 from that result. Thus, if today is 2 in RS, we need 0 in SQLite; if today is 1 in RS we need 6 in SQLite.
      // The easiest way to do that is to use a mod: (app.dateStartOfWeek+5) mod 7
      // Finally, once we've found the end date of this week, we simply subtract six from it to get the start date of this week.
      // For the end date, we simply don't subtract the six.  [/code]

[quote=167303:@Massimo Valle]Your code is wrong.
It add a day only when ThisMonth.DayOfWeek=1 (Sunday)

This is what I’d write:

if ThisMonth.DayOfWeek <> 1 and ThisMonth.DayOfWeek <> 7 and ThisMonth.Day <> 4 // Add all days: Monday thru Friday End If[/quote]
That is also wrong. He only wanted to exclude publuc holidays like the 4th of July, not the 4th of every month :wink:

For fixed calendar dates (eg. july 4th):

Populate an sqlite calendar table, insert holidays for that year, check if such date is IN that list, skip.

System managers from any country can set their own holidays populating the “holidays_calendar” table.

For skiping sundays/saturdays a simple: IF ThisMonth.DayOfWeek = 1 OR ThisMonth.DayOfWeek = 7 Then //Skip

No, it’s not. It’s supposed he is handling the month of July.
Btw, the above approach is wrong for handling a full year of holidays.

Fun idea!

  1. Create an array that contain each day you wish to consider as holidays.
    arrHolidays(0) = “2015-07-04”
    arrHolidays(1) = “2015-12-25”
  2. Ask if DayOfWeek = 6 or 7 or is in the list of the array of holidays. (Make a method, return TRUE or FALSE)
  3. If not, then have another doughnut!

Creative spelling is not good. Try to find a way to avoid it. It’s a challenge, but fun when it works.
However, the user is always right. :slight_smile:

Stupid automatic numbers… That should be 1, 2, 3 and not as shown… I forgot. Excuse me.

Can you elaborate your bad assumption or must I to write an example?

Personally I’d extend a date with methods so you can get away from worrying about it in the presentation and instead do something like:

if not date.isHoliday and not date.isNonWorkingDay then ... end if

Rick, I’ve just fixed the if…end if block from Emile to do what he asked.
I know it only works for the month of July, but perhaps the above block was contained in something like:

[code]if ThisMonth.Month = 7 then

// Emile’s code

end if[/code]

or perhaps not, I don’t know and you neither.
I made no assumption, I just followed what Emile assumed.

In any case, this is not the best approach.

To answer the OP, No, this is not something you can do with a simple If/Then/Else. It gets complex.

Just for understanding. Enhance and adapt. https://drive.google.com/file/d/0B6w6JZen_vW2bm5lYW5pc21vX0U/view?usp=sharing

Hi all,

you seems to need more data on what I wanted to do and how. So here I am:

I have a large Select Case with… 12 cases, one for each month of the year. I took as an example July because it have the 4th… of July: Independence day. But it can be any month THAT have one special non working day in it.

Because my code populates a PopupMenu that will be used to choose a day number, I exclude to put in that PopMenu every Saturdays and Sundays and any other non working day number. So at first I only excluded the Saturdays and Sundays with a simple If block.

At last, I found the code I already shared far above to deal with my needs.

In between, I tested:

// Exclude July 4th, Sundays and Saturdays If ThisMonth.Day = 4 Or ThisMonth.DayOfWeek < 2 And ThisMonth.DayOfWeek < 7 Then PM_Days.AddRow Str(LoopIdx) // LoopIdx is the loop index, this code is in a Loop End If

BUT: that ode does not worked at all. When I get the now working code I was thinking that I have to find a single If …/… Then line that worked instead of what I get and that was the question.

Using this code let me hide to the users all the calendar hassles: this goes silently and the user only sees what he can choose.

Is it clearer now ?

I think you want something like

if not (ThisMonth.Day = 4 or ThisMonth.DayOfWeek < 2 or ThisMonth.DayOfWeek > 6) Then
   PM_Days.AddRow Str(ThisMonth.Day)
End If

Tim:

I do not think I tried that; will do later today.

[quote=167537:@Emile Schwarz]
In between, I tested:

// Exclude July 4th, Sundays and Saturdays If ThisMonth.Day = 4 Or ThisMonth.DayOfWeek < 2 And ThisMonth.DayOfWeek < 7 Then PM_Days.AddRow Str(LoopIdx) // LoopIdx is the loop index, this code is in a Loop End If[/quote]

As I already said, the code above CAN’T work:

try this instead:

if ThisMonth.DayOfWeek <> 1 and ThisMonth.DayOfWeek <> 7 and ThisMonth.Day <> 4 // Add all days: Monday thru Friday End If

I find that a Select statement is much easier to understand than a compound if/elseif block in cases where the expressions being tested don’t follow a uniform syntax:

Select Case True // Finds the first CASE expression the evaluates to TRUE
Case ThisMonth.DayOfWeek = 1, ThisMonth.DayOfWeek = 7 // multiple conditions separated by a comma
   ' weekend
Case ThisMonth.Month = 7 And ThisMonth.Day = 4
   ' July 4
Case [...]

End Select

It works fine, thank you.

Maybe, but will you place a Select Case inside each 12 cases of a larger Select Case ?

Select Case have 12 cases 1 for each month of the year.
An If block is inside each case / each month of the large Select Case.