Results 1 to 8 of 8
  1. #1
    Sidran is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2014
    Posts
    18

    Auto populate table with dates

    I have a table with a few fields, which include a field called StartDate and a field called EndDate. From the table I plan on finding the Min StartDate and the Max EndDate, is it possible, to make a table from these 2 sets of data and have the fields in between be auto populated in increments of a month?

    For example. (Dates in dd/mm/yyyy format)
    StartDate=1/1/2014 and Max EndDate=1/12/2014
    So the table should show:
    1/1/2014
    1/2/2014
    1/3/2014
    1/4/2014
    1/5/2014
    1/6/2014
    1/7/2014
    1/8/2014
    1/9/2014
    1/10/2014
    1/11/2014


    1/12/2014

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    This will require VBA code to create records, like:

    Dim newDate As Date
    newDate = Me.StartDate
    Do Until newDate > Me.EndDate
    CurrentDb.Execute "INSERT INTO tablename(datefield) VALUES(#" & newDate & "#")
    newDate = newDate + 1
    Loop
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Sidran is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Hmmm, figured it would require a loop. What if, instead of adding 1 day to the date, I wanted to add a month to the date? By the way, I started working on a loop before looking at the reply, this is where I've got to:

    Dim Db As DAO.Database
    Dim Recordset As DAO.Recordset


    Set Db = CurrentDb
    Set Recordset = Db.OpenRecordset("Monthly Report Hidden Table1") <------ The table that has the field called "Dates"
    MinStartDate = Me.MinOfStartDate
    MaxEndDate = Me.MaxOfEndDate
    While MinStartDate <= MaxEndDate
    Recordset.AddNew
    Recordset("Dates").Value = MinStartDate
    Recordset.Update
    Me.MinOfStartDate = DateAdd("m", 1, MinStartDate)

    Sadly the last line, where i tried to add a month, didn't work. I am very new to VBA so I doubt my code works. If it doesn't I'll try the one you suggested.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Sorry, I forgot you wanted just the first of each month. Just replace 1 line in my code:

    newDate = DateAdd("m",1,newDate)

    However, you are not showing dates in Access standard.

    Review: http://allenbrowne.com/ser-36.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Sidran is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Entering this give me an error:

    Dim newDate As Date
    newDate = Me.MinOfStartDate
    Do Until newDate > Me.MaxOfEndDate
    CurrentDb.Execute "INSERT INTO "Monthly Report Hidden Table1(Dates) VALUES #" & newDate & "#"
    newDate = DateAdd("m", 1, newDate)
    Loop
    End Sub


    In the above code, Monthly Report Hidden Table1 is my table name (yes there are spaces), Dates is the name of the field in the talbe, MinStartDate is the start date and MaxEndDate is the end date.

    The error is an 'Expected end of statement'. Removing the speech mark before 'Monthly' results in a syntax error.

    About the dates, I did change the setting in the control panel to dd/mm/yyyy and they do appear in my database in the same format. So I doubt that should be the cause of any problem.

  6. #6
    Sidran is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Nevermind the above text. I don't know the reason, but I used your code and instead of using the INSERT INTO, I use the AddNew command to get the job done. Thanks a lot for your help. Seems like you know quite a bit about access cause you seem to be answering everyone problems xD.

  7. #7
    Sidran is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2014
    Posts
    18
    To anyone who is interested in the code that I used:

    Dim Db As Database
    Dim Recordset As Recordset
    Dim MinStartDate As Date


    Set Db = CurrentDb
    Set Recordset = Db.OpenRecordset("TableName")
    MinStartDate = Me.MinOfStartDate


    Do Until MinStartDate > Me.MaxOfEndDate
    Recordset.AddNew
    Recordset("FieldName").Value = MinStartDate
    Recordset.Update
    MinStartDate = DateAdd("m", 1, MinStartDate)
    Loop

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The correct syntax in my code would be:

    CurrentDb.Execute "INSERT INTO [Monthly Report Hidden Table1] (Dates) VALUES(#" & newDate & "#)"

    Enclose names with spaces or special characters/punctuation (underscore is exception) with [].
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Auto populate a table
    By keiath in forum Access
    Replies: 5
    Last Post: 02-16-2014, 01:56 PM
  2. Auto Populate multiple dates in form
    By ACKA200 in forum Forms
    Replies: 4
    Last Post: 04-21-2013, 07:06 AM
  3. Replies: 2
    Last Post: 08-22-2012, 07:59 AM
  4. Auto-populate from another table
    By Palladian1881 in forum Access
    Replies: 2
    Last Post: 08-16-2011, 11:29 AM
  5. auto populate and table relationships
    By jmarti57 in forum Access
    Replies: 0
    Last Post: 11-09-2008, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums