[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create table with 1 year of dates 1 week apart

Posted on 2014-03-20
9
Medium Priority
?
345 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 600 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 46

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 52

Accepted Solution

by:
Gustav Brock earned 1400 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 52

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 52

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 52

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

872 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