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 theres 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 cant 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
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
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.
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.
[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