Here's the code to create and load the month table.
0) As always, back up your database before attempting this.
1) Create a new module, and paste this code in.
2) Change the constants at the top.
* dtFirstStart should be the first day of the first possible month on your database.
* dtFirstEnd should be the last day of that month.
* nbrOfMonths should be the number of months you want to load.
3) Then open the immediate window, type "Call CreateAndLoadMonths", and hit enter.
4) Review the table that has been created and see that it looks reasonable.
Code:
Option Compare Database
Option Explicit
Const dtFirstStart As Date = #1/1/2011#
Const dtFirstEnd As Date = #1/31/2011#
Const nbrOfMonths As Integer = 120
Sub CreateAndLoadMonths()
Call CreateMonthTable
Call LoadMonthDates
End Sub
Sub CreateMonthTable()
Dim db As DAO.Database
Dim myTable As DAO.TableDef
Dim myField As DAO.Field
Dim myProp As DAO.Property
Set db = CurrentDb
' create the new tabledef
Set myTable = db.CreateTableDef("tblMonths")
' add the two date fields to the tabledef
With myTable
.Fields.Append .CreateField("mthStartDt", dbDate)
.Fields.Append .CreateField("mthEndDt", dbDate)
End With
' create the table in the database using the tabledef
db.TableDefs.Append myTable
'update the format of the start date
Set myField = myTable.Fields("mthStartDt")
Set myProp = myField.CreateProperty("Format", dbText, "Short Date")
myField.Properties.Append myProp
'update the format of the end date
Set myField = myTable.Fields("mthEndDt")
Set myProp = myField.CreateProperty("Format", dbText, "Short Date")
myField.Properties.Append myProp
'Refresh the nav panel
Application.RefreshDatabaseWindow
' explicitly kill the objects
Set myProp = Nothing
Set myField = Nothing
Set myTable = Nothing
Set db = Nothing
End Sub
Sub LoadMonthDates()
Dim dtMthStart As Date
Dim dtMthEnd As Date
Dim intI As Integer
Dim strSQL As String
Dim db As DAO.Database
Set db = CurrentDb
' kill all current records
strSQL = "DELETE FROM tblMonths;"
db.Execute (strSQL)
' add one record per month
For intI = 0 To nbrOfMonths
dtMthStart = DateAdd("m", intI, dtFirstStart)
dtMthEnd = DateAdd("m", intI, dtFirstEnd)
strSQL = "INSERT INTO tblMonths (mthStartDt, mthEndDt) " & _
"VALUES ( #" & dtMthStart & "#, #" & dtMthEnd & "# );"
db.Execute (strSQL)
Next intI
' explicitly kill the objects
Set db = Nothing
End Sub