Solved

Create table with 1 year of dates 1 week apart

Posted on 2014-03-20
9
336 Views
Last Modified: 2014-04-09
Hi,

With a given base date say of 1/1/2014, I would like to create a record set of 52 records incrementing the date by 1 week in each record so it is the same day-of-week in each date (taking Leap year into account).  Each record also needs to be consecutively numbered beginning with 1 on the base date and 52 on the last.

Macro's are nice but I think this needs to be VBA.  

Help is appreciated.
Charlie
0
Comment
Question by:cwbarrett
9 Comments
 
LVL 20

Assisted Solution

by:GrahamMandeno
GrahamMandeno earned 150 total points
ID: 39944267
Hi Charlie

Some code like this should do the trick:
Dim db As DAO.Database
Dim iWeek As Integer
Dim dtWeek As Date
Set db = CurrentDb
dtWeek = DateSerial(2014, 1, 1)
For iWeek = 1 To 52
    db.Execute "INSERT INTO [YourTable] ([NumField], [DateField]) " _
        & "VALUES (" & iWeek & ", " & Format(dtWeek, "\#yyyy-mm-dd\#") & ");"
    dtWeek = dtWeek + 7
Next iWeek

Open in new window

Graham Mandeno [Access MVP 1996-2014]
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39944305
You can also use a tally/number table for this, as I show in this article:
http://www.experts-exchange.com/A_5410.html

I showed the example of sequential dates in the article.  You can multiply the number table values by 7 to get the week intervals you seek.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 350 total points
ID: 39944579
For such a loop you would, of course, use DAO:
Public Sub PopulateWeeks(ByVal datStart As Date)

    Const clngWeeks As Long = 52
    
    Dim dbs         As DAO.Database
    Dim rst         As DAO.Recordset
    
    Dim lngWeek     As Long
    
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("select * from tblWeek")
    
    For lngWeek = 1 To clngWeeks
        rst.AddNew
            rst!Id.Value = lngWeek
            rst!WeekDate = DateAdd("ww", lngWeek - 1, datStart)
        rst.Update
    Next
    rst.Close
    
    Set rst = Nothing
    Set dbs = Nothing
    
End Sub

Open in new window

You will have to adjust table and field names to those of yours.

/gustav
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:cwbarrett
ID: 39956835
Thank you both for responding, however, its' going to be a week or 2 before I can test the solutions.  I will get back to this.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39956838
No problem.

/gustav
0
 

Author Closing Comment

by:cwbarrett
ID: 39989521
Thank you both for the solution.  One seemed easier but didn't account for leap year.  I think I can come up with a solution that uses both.

Thank you!
Charlie
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39989533
You are welcome!

You could use DateAdd with the db.Execute method to account for leap years, but why would you? DAO is much faster here and does it all.

/gustav
0
 

Author Comment

by:cwbarrett
ID: 39989539
You're saying this will execute faster?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39989551
Yes. 52 calls of db.execute is not very fast.

But why don't you try? It shouldn't take you more that a few minutes ...

/gustav
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now