Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Display First day of week

1 view
Skip to first unread message

Dan

unread,
Oct 20, 2005, 9:47:03 PM10/20/05
to
I have a report with data for every day of the week beginning with the
10/20/05 format and then my other record data. I currently use the
=Format([Date],"ww") to display the week when I summarize on my report (on
Date Footer). Using the same [Date] feild how do I display the first day of
the week (Monday) in the date fomat "10/17/05" on the same summary line in
the Date Fotter section? Thx.
--
Dan

James A. Fortune

unread,
Oct 20, 2005, 10:56:02 PM10/20/05
to

Here's how I would go about it. I can use the optional parameter of the
Weekday function or just lay out what I want:

I'd like to format something like:
DateAdd("d", SomeNumber, [DateField])

To get SomeNumber:
Weekday, SomeNumber
2 => 0
3 => -1
4 => -2
5 => -3
6 => -4
7 => -5
1 => -6

SomeNumber = -(X + 5) Mod 7 works. After checking that Access doesn't
do anything unexpected with the Mod function (it doesn't):

=Format(DateAdd("d", -(Weekday([DateField]) + 5) Mod 7, [DateField]),
"mm/dd/yy")

should do it. I used a different name for the date field since Date is
a reserved word in Access. Just replace DateField in both places with
the new name you are using.

James A. Fortune

Ken Snell [MVP]

unread,
Oct 20, 2005, 11:02:16 PM10/20/05
to
Put this function in a regular module:


Public Function DateOfSpecificWeekDay(ByVal OriginalDate As Date, _
ByVal intWeekDay As Integer) As Date
' ** THIS FUNCTION RETURNS THE DATE OF THE SPECIFIC DAY OF THE WEEK
' ** IN WHICH THE ORIGINAL DATE IS.
' ** intWeekDay = 1 is Sunday, 2 is Monday, etc.
On Error Resume Next
DateOfSpecificWeekDay = DateAdd("d", -DatePart("w", OriginalDate, _
1) + intWeekDay, OriginalDate)
Err.Clear
End Function


Then use it as the Control Source in the textbox to show you the desired
date:
=DateOfSpecificWeekDay(Date(), 2)


Or you could just use this expression as the Control Source of your textbox:
= DateAdd("d", -DatePart("w", Date(), 1) + 2, Date())


--

Ken Snell
<MS ACCESS MVP>


"Dan" <D...@discussions.microsoft.com> wrote in message
news:79D0A0B6-5FC6-4BF7...@microsoft.com...

Dan

unread,
Oct 20, 2005, 11:08:01 PM10/20/05
to
James, thanks your solution worked great. Dan
--
Dan

Dan

unread,
Oct 20, 2005, 11:15:06 PM10/20/05
to
Brilliant. I will give it a go. Dan
--
Dan

Aaron

unread,
Aug 2, 2007, 8:56:00 PM8/2/07
to
This function works wonderfully! What I'm trying to do next is to have an
excel sheet linked to my query containing the above formula. However, the
MSQuery browser doesn't like the formula and says "data type mismatch".

is there a way to manipulate the above query to allow me to have excel link
to it properly ?

Ken Snell (MVP)

unread,
Aug 7, 2007, 6:47:47 PM8/7/07
to
I've not worked with MSQuery, so I cannot give a firm answer. However, you
could replace the function call in the query with the entire expression that
is used in the function (I'm assuming that you want to use Sunday as the
first day of the week, so I put a 1 in for the intWeekDay value in the
expression):

TheWeekDayNumber: DateAdd("d", -DatePart("w", [DateFieldName], 1) + 1,
[DateFieldName])

--

Ken Snell
<MS ACCESS MVP>

"Aaron" <Aa...@discussions.microsoft.com> wrote in message
news:038D0FC9-1551-4086...@microsoft.com...

raskew via AccessMonster.com

unread,
Aug 7, 2007, 8:39:12 PM8/7/07
to
Today is Tuesday 7 Aug 2007. Try this to obtain the Sunday of this week:

? date() - weekday(date()) + 1
8/5/2007

Bob


Ken Snell (MVP) wrote:
>I've not worked with MSQuery, so I cannot give a firm answer. However, you
>could replace the function call in the query with the entire expression that
>is used in the function (I'm assuming that you want to use Sunday as the
>first day of the week, so I put a 1 in for the intWeekDay value in the
>expression):
>
>TheWeekDayNumber: DateAdd("d", -DatePart("w", [DateFieldName], 1) + 1,
>[DateFieldName])
>

>> This function works wonderfully! What I'm trying to do next is to have an
>> excel sheet linked to my query containing the above formula. However, the

>[quoted text clipped - 38 lines]


>>> > > line in
>>> > > the Date Fotter section? Thx.

--
Message posted via http://www.accessmonster.com

Aaron

unread,
Aug 9, 2007, 12:40:11 AM8/9/07
to
That's exactly what I've done. The query works perfectly with Access, but
when I try using the data/import external data/ New database query, I get the
"data type mismatch" error.

Ken Snell (MVP)

unread,
Aug 9, 2007, 1:57:03 PM8/9/07
to
I have created a query in an ACCESS file that uses the expression as a
calculated field. I then used EXCEL as you indicated to return the results
of that query, and did not get a "data type mismatch" error. I tried it with
and withou sorting/filtering options when linking to the query from EXCEL.

Can you give us the actual SQL statement of the ACCESS query, and the exact
steps that you used to link to the query in EXCEL?
--

Ken Snell
<MS ACCESS MVP>

"Aaron" <Aa...@discussions.microsoft.com> wrote in message

news:29429737-D60A-4747...@microsoft.com...

Aaron

unread,
Aug 15, 2007, 6:12:00 PM8/15/07
to
So, after researching further, it appears that when my date I am looking up
is null, the expression returns an error, which gives me a "data type
mismatch" error when exporting to excel...

after I put in an if statement to catch this, it works like a charm.

Thanks much Ken

Ken Snell (MVP)

unread,
Aug 15, 2007, 7:59:52 PM8/15/07
to
"Aaron" <Aa...@discussions.microsoft.com> wrote in message
news:2443C826-96AB-4EB8...@microsoft.com...

> So, after researching further, it appears that when my date I am looking
> up
> is null, the expression returns an error, which gives me a "data type
> mismatch" error when exporting to excel...

Yes, that indeed will cause that error < smile >.

>
> after I put in an if statement to catch this, it works like a charm.

Good work!

>
> Thanks much Ken

You're welcome.

0 new messages