Single Month Format for 3 months' dates

K

KarenY

I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KARL DEWEY

Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of the
month?
 
K

Ken Sheridan

Karen:

I'd suggest that you use an auxiliary 'calendar' table. This is simply a
table of all dates within the period of time your database is likely to cover
(though its simple to extend it later). As well as the date column, CalDate,
you include other columns which hold information about each date. In your
case you'd have a column AcctgMonth. You can use a text data type , with
values January, February etc or an integer number data type with values 1,2
etc, whichever you prefer. The latter allows for sorting in month order and
its easy to return the name of the month from a number with Format("2000/" &
Format(AcctMonth,"00"),"mmmm"), so I'd suggest using an integer number data
type. The choice of year 2000 is entirely arbitrary here BTW, any year would
do.

An easy way to create calendar table is to serially fill down a column in
Excel with dates and then import it into Access as a table. Then define the
CalDate column as the primary key and add the other columns. You can also
do it by running a VBA procedure, which I'll give you at the end of this post.

Once you have your Calendar table with a populated CalDate column and an
empty AcctgMonth column you can fill the latter with a series of simple
update queries, e.g. for July 2008:

UPDATE Calendar
Set AcctgMonth = 7
WHERE CalDate BETWEEN
#06/29/2008# AND #08/02/2008#;

You don't need to save these, just paste the above into a new query in SQL
view and run it. Then change the SQL for another month and run that, and so
on. You don't need to update all the rows into the future, only as far as
you need to at present. You can run more update queries later to fill the
future dates' rows.

You might need another column AcctYear if the January and December
accounting months don't star/end at 1 January and 31 December. You'd fill
this column in a similar way with update queries, but you only need one per
year of course.

Armed with this table you can now join it to your main table, which I'll
call Transactions for this example, to return the accounting month for each
row, e.g. to return rows for accounting months May, June and July 2008:

SELECT Transactions.*, Calendar.AcctYear, Calendar.AcctMonth,
FORMAT("2000/" & Format(AcctMonth,"00"),"mmmm") AS AcctMonthName
FROM Transactions INNER JOIN Calendar
ON Transactions.Transactiondate = Calendar.CalDate
WHERE Calendar.AcctYear = 2008 AND Calendar.AcctMonth IN (5,6,7);


Finally here's the function to create the basic calendar table:

Public Function MakeCalendar_DAO(strTable As String, _
dtmStart As Date, _
dtmEnd As Date, _
ParamArray varDays() As Variant)

' Accepts: Name of calendar table to be created: String.
' Start date for calendar: DateTime.
' End date for calendar: DateTime.
' Days of week to be included in calendar
' as value list, e,g 2,3,4,5,6 for Mon-Fri
' (use 0 to include all days of week)

Dim dbs As DAO.Database, tdf As DAO.TableDef
Dim strSQL As String
Dim dtmDate As Date
Dim varDay As Variant
Dim lngDayNum As Long

Set dbs = CurrentDb

' does table exist? If so get user confirmation to delete it
On Error Resume Next
Set tdf = dbs.TableDefs(strTable)
If Err = 0 Then
If MsgBox("Replace existing table: " & _
strTable & "?", vbYesNo + vbQuestion, _
"Delete Table?") = vbYes Then
strSQL = "DROP TABLE " & strTable
dbs.Execute strSQL
Else
Exit Function
End If
End If
On Error GoTo 0

' create new table
strSQL = "CREATE TABLE " & strTable & _
"(CalDate DATETIME, " & _
"CONSTRAINT PrimaryKey PRIMARY KEY (CalDate ))"
dbs.Execute strSQL

' refresh database window
Application.RefreshDatabaseWindow

If varDays(0) = 0 Then
' fill table with all dates
For dtmDate = dtmStart To dtmEnd
lngDayNum = lngDayNum + 1
strSQL = "INSERT INTO " & strTable & "(CalDate ) " & _
"VALUES(#" & Format(dtmDate, "mm/dd/yyyy") & "#)"

dbs.Execute strSQL
Next dtmDate
Else
' fill table with dates of selected days of week only
For dtmDate = dtmStart To dtmEnd
For Each varDay In varDays()
If Weekday(dtmDate) = varDay Then
lngDayNum = lngDayNum + 1
strSQL = "INSERT INTO " & strTable & "(CalDate ) " & _
"VALUES(#" & Format(dtmDate, "mm/dd/yyyy") & "#)"
dbs.Execute strSQL
End If
Next varDay
Next dtmDate
End If

End Function

Just paste the above into any standard module in your database. Say you
want to create a calendar for 2008 – 2010 you'd call it like so:

MakeCalendar "Calendar", #01/01/2008#, #12/31/2010#, 0

You can enter this in the debug window (aka immediate window) which you can
open by pressing Ctrl + G.

This will create the table with the just the one column CalDate . You can
then amend its design to add the AcctgMonth column and update it as described
above.

Ken Sheridan
Stafford, England

KarenY said:
I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KarenY

No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of the
month?

--
KARL DEWEY
Build a little - Test a little


KarenY said:
I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KARL DEWEY

Try this - Create an accounting calendar like this --
Text DateTime DateTime
AcctMonth Start End
200806 5/24/2008 6/28/2008
200807 6/29/2008 8/2/2008
200808 8/3/2008 8/30/2008
200809 8/31/2008 9/27/2008

Then this in your query ---
SELECT YourTable.*
FROM AcctMonth, YourTable
WHERE (((YourTable.YourDate) Between [Start] And [End]) AND
((AcctMonth.AcctMonth)=[Enter your Year and Month - yyyymm]));

--
KARL DEWEY
Build a little - Test a little


KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of the
month?

--
KARL DEWEY
Build a little - Test a little


KarenY said:
I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
B

BruceM

I was intrigued (and this is something that could come up before long in one
of my projects), so I started working on something. These expressions
should give you the start and end of a fiscal month:

DateAdd("d",-Weekday(#7/1/08#)+1,#7/1/08#)
DateAdd("d",7 - Weekday(#7/31/08#),#7/31/08#)

In both cases I have entered the start and end dates directly into the
expressions. I am still experimenting with a function that will take a
given month and year and calculate the start of the fiscal month, and
similarly calculate the end of the fiscal month from another given month and
year. In the hard-coded example above the expressions return 6/29/2008 as
the start of the July fiscal month and 8/2/2008 as the end of the July
fiscal month. My understanding is that if the user enters "July" you want
to see records from 6/29 through 8/2. Is that correct?

I will not be able to return to this until some time tomorrow.

KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the
year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of
the
month?

--
KARL DEWEY
Build a little - Test a little


KarenY said:
I want to run a query for the monthly data. The table only supplies me
the
date (no month). Our Accounting Month does not work along with the
Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd
August
2008.

I can make the month either with PLUS day or MINUS day for the month
across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether
it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if
no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KarenY

I will try this in the morning in the office ! thanks a lot

KARL DEWEY said:
Try this - Create an accounting calendar like this --
Text DateTime DateTime
AcctMonth Start End
200806 5/24/2008 6/28/2008
200807 6/29/2008 8/2/2008
200808 8/3/2008 8/30/2008
200809 8/31/2008 9/27/2008

Then this in your query ---
SELECT YourTable.*
FROM AcctMonth, YourTable
WHERE (((YourTable.YourDate) Between [Start] And [End]) AND
((AcctMonth.AcctMonth)=[Enter your Year and Month - yyyymm]));

--
KARL DEWEY
Build a little - Test a little


KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of the
month?

--
KARL DEWEY
Build a little - Test a little


:

I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KarenY

I think this should work ! Thanks a lot !

KARL DEWEY said:
Try this - Create an accounting calendar like this --
Text DateTime DateTime
AcctMonth Start End
200806 5/24/2008 6/28/2008
200807 6/29/2008 8/2/2008
200808 8/3/2008 8/30/2008
200809 8/31/2008 9/27/2008

Then this in your query ---
SELECT YourTable.*
FROM AcctMonth, YourTable
WHERE (((YourTable.YourDate) Between [Start] And [End]) AND
((AcctMonth.AcctMonth)=[Enter your Year and Month - yyyymm]));

--
KARL DEWEY
Build a little - Test a little


KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of the
month?

--
KARL DEWEY
Build a little - Test a little


:

I want to run a query for the monthly data. The table only supplies me the
date (no month). Our Accounting Month does not work along with the Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd August
2008.

I can make the month either with PLUS day or MINUS day for the month across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
K

KarenY

Thank you, BruceM.

That's correct. I want the users to input July to include the records
between 6/29 and 8/2. I would like to learn that too (it sounds like more
simple if it can be worked directly on the qry from the table).
I would appreciate if you could explain a bit more in details for me.

Your have two expressions there, where do I put them. (sorry)
In my query (design view):
I added a field with a criteria at the bottom [Enter month] for the users to
input the parameter value (e.g.) July.
In the field (field name is AcctgMonth) -
AcctgMonth: Format$([Date]+[] And -[],'mmmm')

("Date" is the field's name in the table, yes, I know it's a bad idea to use
"date" for the field, but the original file was not mine.)

I tried to put both expressions in the criteria (two rows) under the [Enter
month] in the field "AcctgMonth". I know I must have done wrong.
The query has picked up the records of 8/2 but the AcctgMonth shows August
instead of July and it has not picked up records of 6/30 either (there is
zero record on 6/29 in the table).

thanks
Karen



BruceM said:
I was intrigued (and this is something that could come up before long in one
of my projects), so I started working on something. These expressions
should give you the start and end of a fiscal month:

DateAdd("d",-Weekday(#7/1/08#)+1,#7/1/08#)
DateAdd("d",7 - Weekday(#7/31/08#),#7/31/08#)

In both cases I have entered the start and end dates directly into the
expressions. I am still experimenting with a function that will take a
given month and year and calculate the start of the fiscal month, and
similarly calculate the end of the fiscal month from another given month and
year. In the hard-coded example above the expressions return 6/29/2008 as
the start of the July fiscal month and 8/2/2008 as the end of the July
fiscal month. My understanding is that if the user enters "July" you want
to see records from 6/29 through 8/2. Is that correct?

I will not be able to return to this until some time tomorrow.

KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout the
year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

KARL DEWEY said:
Does you accounting moth always include two days from the prior month or
those days of the prior month that are in the same week of the start of
the
month?

--
KARL DEWEY
Build a little - Test a little


:

I want to run a query for the monthly data. The table only supplies me
the
date (no month). Our Accounting Month does not work along with the
Calendar
Month, for instance, July 2008 starts with 29th June and ends with 2nd
August
2008.

I can make the month either with PLUS day or MINUS day for the month
across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know whether
it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month, if
no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 
B

BruceM

As I though about this I realized that one of the things I'm not getting
here is what happens to August, for instance. If the July accounting month
includes all days in July plus portions of June and August, what is the
August accounting month? If it starts on August 3 (the start of the first
full week in August), why does the July accounting month not similarly start
on Sunday of the first full month in July (July 6)? If the July accounting
month is June 29 through August 2, by the same logic the August accounting
month is July 27 through September 6, which means July 27-31 would be
included in two accounting months.

I should have been clearer, but it was the end of the day. To view the code
results, use the VBA editor's Immediate Window. To see this, open any
database and press Ctrl + G. This will open the VBA editor, and will show
the Immediate Window at the bottom (it's labeled "Immediate"). Type this in
the Immedeate Window (be sure to include the questin mark):
?DateAdd("d",8-Weekday(#7/1/08#),#7/1/08#)
Press the Enter key. It should show you July 6 directly below the
expression.
Experiment with other months, but note that a tweak is needed for June 2008
or any month that starts on Sunday.
Try this:
?DateAdd("d",7 - Weekday(#7/31/08#),#7/31/08#)
I think this will work for any month to show you the end of the Accounting
Month (August 2 in the case of July).

Check Help for more information about the DateAdd and Weekday functions. In
brief, DateAdd lets you add an interval (day, week, month, quarter, etc.) to
a date. You can also subtract an interval. Weekday returns a number for
the day of the week. If the date is Sunday it returns 1; Monday is 2,
etc.).

To make this into a function, in the VBA editor (which you are in if you are
using the Immediate Window), click Insert >> Module from the menu bar. You
should see a blank section with:
Option Compare Database
Option Explicit

If you do not see Option Explicit, type it in. To make it show up in the
future, click Tools > Options. On the Editor tab check Require Variable
Declaration. I won't get into details about that just now, but I will say
that variable declaration can be a great help in finding code problems.

Skip a line for readability, then add:

Public datMoStart As Date, datMoEnd As Date, _
datFMoStart As Date, datFMoEnd As Date

Skip another line or two, then add:

Public Function AcctMoStart(StartMonth As Integer, StartYear As Integer) As
Date

datMoStart = DateSerial(StartYear, StartMonth, 1)

If Weekday(datMoStart) = 1 Then
datFMoStart = datMoStart
Else
datFMoStart = DateAdd("d", 8 - Weekday(datMoStart), datMoStart)
End If

AcctMoStart = datFMoStart

End Function


Public Function AcctMoEnd(EndMonth As Integer, EndYear As Integer) As Date

datMoEnd = DateSerial(EndYear, EndMonth + 1, 0)

datFMoEnd = DateAdd("d", 7 - Weekday(datMoEnd), datMoEnd)

AcctMoEnd = datFMoEnd

End Function

Test it in the Immediate Window. For instance, to see the start of the July
2008 accounting month:
?AcctMoStart(7,2008)

Close the code module. You will be prompted for a name. Name it something
like mdlGeneralCode.

In the criteria row for the date field in the query:
Between AcctMoStart([Start Month],[Start Year]) And AcctMoEnd([End
Month],[End Year])

The user will be prompted for a Start Month (they need to enter a number),
Start Year, End Month (number), and End Year.

If this works as intended you can use a form to make it easier to specify
the criteria, but that can wait.

If I have misunderstood the start of the accounting month the formula can be
adjusted.

KarenY said:
Thank you, BruceM.

That's correct. I want the users to input July to include the records
between 6/29 and 8/2. I would like to learn that too (it sounds like more
simple if it can be worked directly on the qry from the table).
I would appreciate if you could explain a bit more in details for me.

Your have two expressions there, where do I put them. (sorry)
In my query (design view):
I added a field with a criteria at the bottom [Enter month] for the users
to
input the parameter value (e.g.) July.
In the field (field name is AcctgMonth) -
AcctgMonth: Format$([Date]+[] And -[],'mmmm')

("Date" is the field's name in the table, yes, I know it's a bad idea to
use
"date" for the field, but the original file was not mine.)

I tried to put both expressions in the criteria (two rows) under the
[Enter
month] in the field "AcctgMonth". I know I must have done wrong.
The query has picked up the records of 8/2 but the AcctgMonth shows August
instead of July and it has not picked up records of 6/30 either (there is
zero record on 6/29 in the table).

thanks
Karen



BruceM said:
I was intrigued (and this is something that could come up before long in
one
of my projects), so I started working on something. These expressions
should give you the start and end of a fiscal month:

DateAdd("d",-Weekday(#7/1/08#)+1,#7/1/08#)
DateAdd("d",7 - Weekday(#7/31/08#),#7/31/08#)

In both cases I have entered the start and end dates directly into the
expressions. I am still experimenting with a function that will take a
given month and year and calculate the start of the fiscal month, and
similarly calculate the end of the fiscal month from another given month
and
year. In the hard-coded example above the expressions return 6/29/2008
as
the start of the July fiscal month and 8/2/2008 as the end of the July
fiscal month. My understanding is that if the user enters "July" you
want
to see records from 6/29 through 8/2. Is that correct?

I will not be able to return to this until some time tomorrow.

KarenY said:
No, unfortunately not. Our Accounting months run by
4 weeks for 3 months, then 5 weeks for a month, and so on throughout
the
year.
I just hope a simple function format to work in that particular month
(usually it happens once a year).

:

Does you accounting moth always include two days from the prior month
or
those days of the prior month that are in the same week of the start
of
the
month?

--
KARL DEWEY
Build a little - Test a little


:

I want to run a query for the monthly data. The table only supplies
me
the
date (no month). Our Accounting Month does not work along with the
Calendar
Month, for instance, July 2008 starts with 29th June and ends with
2nd
August
2008.

I can make the month either with PLUS day or MINUS day for the month
across
with 2 months. which does not include the dates of 3 months :

I have a criteria for pop up parameter value for the user to input
the
month, e.g. July,
then I made in the "field" with another parameter pops up to fill
out 1
(i.e. plus 1 day). It works out fine to have June included.
Since this July includes the 1st 2 days in August, I don't know
whether
it's
poslsible to combine the "minus" in the following function :

AcctgMonth: Format$([Date]+[Enter Plus days betw MEC and cal-month,
if
no
diff, enter 0],'mmmm')

Please help.
thanks
Karen
 

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