Weekday code function

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
 
J

John Vinson

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)
 
W

Wayne Morgan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top