Determine which day of the week (1 Viewer)

deejay_totoro

Registered User.
Local time
Today, 14:35
Joined
May 29, 2003
Messages
169
Hello,

I want to set a date in the field [MyFieldStartDate], from which queries will be run. The start date cannot be on a Saturday or Sunday. (For the moment I am not concerned with national holidays.)

If someone could help, that would be great!

Here is psuedo code if what I want to do:

If strToday = (Saturday or Sunday) Then Set the date of [MyFieldStartDate] to (the date of Friday of last week)

However!

If strToday = Any other day of the week, then [MyFieldStartDate] is (yesterdays date)

Many thanks,

dj_T
 

Matt Greatorex

Registered User.
Local time
Today, 09:35
Joined
Jun 22, 2005
Messages
1,019
If I understand the logic, then the way of handling all days except Sunday is to revert to the previous day? If so, is something like this what you need?

Code:
Dim ld_Today as Date

ld_Today = CDate(strToday) ' -- Converts the string to a date

If Format(ld_Today,"DDDD") = "Sunday") Then 

          [MyFieldStartDate] = DateAdd("d", 1, ld_Today - 2) ' -- 2 days before

Else

          [MyFieldStartDate] = DateAdd("d", 1, ld_Today - 1) ' -- 1 day before

End If
 

billyr

Registered User.
Local time
Today, 09:35
Joined
May 25, 2003
Messages
123
What do you want to happen if strToday is Monday?
 

Matt Greatorex

Registered User.
Local time
Today, 09:35
Joined
Jun 22, 2005
Messages
1,019
billyr said:
What do you want to happen if strToday is Monday?

Fair point :eek: . That's what i get for rushing.

Make that

Code:
Dim ld_Today as Date

ld_Today = CDate(strToday) ' -- Converts the string to a date

If Format(ld_Today,"DDDD") = "Sunday") Then 

          [MyFieldStartDate] = DateAdd("d", 1, ld_Today - 2) ' -- 2 days before

ElseIf Format(ld_Today,"DDDD") = "Monday") Then 

          [MyFieldStartDate] = DateAdd("d", 1, ld_Today - 3) ' -- 3 days before

Else

          [MyFieldStartDate] = DateAdd("d", 1, ld_Today - 1) ' -- 1 day before

End If
 
R

Rich

Guest
What about Saturdays? and why not just use the Weekday function?
 

billyr

Registered User.
Local time
Today, 09:35
Joined
May 25, 2003
Messages
123
Rich is right. Sounds like a case statement would be the simple route.
Select case weekday(ld_today)
case 1
........
case 2
........
case 7
........
case else
........
end select
 

raskew

AWF VIP
Local time
Today, 08:35
Joined
Jun 2, 2001
Messages
2,734
Hi -

You might give this a try:
Code:
Function BackBusDays2(pStart As Date, _
                      pNum As Integer) As Date
'*******************************************
'purpose:   Output date if pNum business
'           days are subtracted from pStart.
'coded by:  raskew
'Inputs:    from debug (immediate) window)
'           ? BackBusDays2(#2/22/06#, 3)
'Output:    2/17/06
'*******************************************
                      
Dim dteHold As Date
Dim i As Integer
Dim n As Integer

    dteHold = pStart - 1
    n = 0
    Do While n < pNum
       i = WeekDay(dteHold)
       n = n + IIf(i = 1 Or i = 7, 0, 1)
       dteHold = dteHold - 1
    Loop
    
    BackBusDays2 = dteHold + 1
End Function

HTH - Bob
 

Matt Greatorex

Registered User.
Local time
Today, 09:35
Joined
Jun 22, 2005
Messages
1,019
Rich said:
What about Saturdays? and why not just use the Weekday function?

As far as Saturday goes, treating it like another day and just using the previous one would work, as it would give the Friday.

Didn't know about the weekday function. :eek:
 

Users who are viewing this thread

Top Bottom