Inserting code to obtain first day of workweek.

S

Steel Bananana

The following code was given to me by Klatuu to calculate the starting day of
the work week.

PUblic Function WeekStartDate(ByVal BaseDate As Date) As Date
'Dave Hargis 9/04
'Returns the first date of the accounting week for the date entered)

WeekStartDate = DateAdd("d", vbSunday - DatePart("w", BaseDate,
vbMonday), BaseDate)
End Function

However, the line beginning 'WeekStartDate=" is coming up red in VB. It's
based on a date field that is autopopulated and is supposed to insert this
date into an invisible 'week' field. However, it's not doing it!!! I tried
using it as an event procedure as the form opens. I was told that it is a
module code, so I created a module, but I have no idea what to do with it!!!
I can send an example of the table and subform...just in case someone is
willing to help one on one.

Sorry for being such a beginner! I'm ripping my hair out trying to get my
data organized by week so I can view ALL ENTRIES MADE for the week (by date)
and then use a drop-down in a form to view previous week's info.
 
K

Ken Snell \(MVP\)

Did you make this code step a single line in your code? It needs to be:

WeekStartDate = DateAdd("d", vbSunday - DatePart("w", BaseDate,
vbMonday), BaseDate)

The newsreader usually wraps text, and so you may have put this code step as
two lines in your code because that's how it looked in the reader.

Or, split the code step across two lines this way, by using the _
continuation character:

WeekStartDate = DateAdd("d", vbSunday - _
DatePart("w", BaseDate, vbMonday), BaseDate)
 

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