Weekday code function

  • Thread starter Thread starter Heather
  • Start date Start date
H

Heather

I inherited a db and am having a hard time understanding
this. The field is called SCHSTART and an example of a
date is 12/31/2003. The code is below. It is being
populated into a new table can any one explain this part
of the code and what the output would be for the date
12/31/2003? THANKS...

If Weekday(rsx!SCHSTART) = 6 Then
rs!we_dt = rsx!SCHSTART + 1
ElseIf Weekday(rsx!SCHSTART) = 5 Then
rs!we_dt = rsx!SCHSTART + 2
ElseIf Weekday(rsx!SCHSTART) = 4 Then
rs!we_dt = rsx!SCHSTART + 3
ElseIf Weekday(rsx!SCHSTART) = 3 Then
rs!we_dt = rsx!SCHSTART + 4
ElseIf Weekday(rsx!SCHSTART) = 2 Then
rs!we_dt = rsx!SCHSTART + 5
ElseIf Weekday(rsx!SCHSTART) = 1 Then
rs!we_dt = rsx!SCHSTART + 6
Else
rs!we_dt = rsx!SCHSTART
End If
 
I inherited a db and am having a hard time understanding
this. The field is called SCHSTART and an example of a
date is 12/31/2003. The code is below. It is being
populated into a new table can any one explain this part
of the code and what the output would be for the date
12/31/2003? THANKS...

If Weekday(rsx!SCHSTART) = 6 Then
rs!we_dt = rsx!SCHSTART + 1
ElseIf Weekday(rsx!SCHSTART) = 5 Then
rs!we_dt = rsx!SCHSTART + 2
ElseIf Weekday(rsx!SCHSTART) = 4 Then
rs!we_dt = rsx!SCHSTART + 3
ElseIf Weekday(rsx!SCHSTART) = 3 Then
rs!we_dt = rsx!SCHSTART + 4
ElseIf Weekday(rsx!SCHSTART) = 2 Then
rs!we_dt = rsx!SCHSTART + 5
ElseIf Weekday(rsx!SCHSTART) = 1 Then
rs!we_dt = rsx!SCHSTART + 6
Else
rs!we_dt = rsx!SCHSTART
End If

This can be done a LOT more simply. The Weekday() function returns a
value of 1 for Sunday, 2 for Monday, ..., 7 for Saturday; this code is
apparently setting the value of we_dt to the Saturday following
whatever date is in SCHSTART.

This can be done in one line, however:

rs!we_dt = DateAdd("d", 7 - Weekday(rsx!SCHSTART), rsx!SCHSTART)
 
Weekday returns the day of the week as a number. In the US, the default
would be Sunday=1, Monday=2, Tuesday=3, etc.

So Weekday(#12/31/2003#) = 4, following through the code, they are then
adding 3 to this day to give you January 3, 2004. It appears that the idea
is to give you the date of the Saturday following the date listed.
 
Back
Top