autodate like autonum in access2003

P

PETER

Is there a way of automatically populating the date field of a basic diary
database I created, so that I can pre-create 365 records with a years worth
of dates?
 
B

Brendan Reynolds

Public Sub CreateDates(ByVal StartDate As Date)

Dim dtmDate As Date

For dtmDate = StartDate To DateAdd("yyyy", 1, StartDate) - 1
CurrentDb.Execute "INSERT INTO tblTest (TestDate) VALUES (#" & _
Format$(dtmDate, "mm/dd/yyyy") & "#)", dbFailOnError
Next dtmDate

End Sub

Where 'tblTest' is the name of your table, and 'TestDate' is the name of
your date/time field.

Example of use, in Immediate Window ...

createdates(date())
 
J

John Vinson

Is there a way of automatically populating the date field of a basic diary
database I created, so that I can pre-create 365 records with a years worth
of dates?

You'll need either VBA, or an auxiliary table - or, you can use Excel
to create the data. Open a spreadsheet; enter the start date and the
day after; and use Fill Down to fill out the year. Copy and paste into
an Access table.

John W. Vinson[MVP]
 
Top