Results 1 to 4 of 4
  1. #1
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110

    Add a range of dates to a table


    I want to create a form that ask the user for a start date and an end date. Then when the click OK it will append a record to a table for each date and each date in between based on the increment. For example the start date is 6/20/2014 and the end date is 6/27/2014 and the increment will be 1 day. I want to add records to my table for:

    6/20/2014
    6/21/2014
    6/22/2014
    6/23/2014
    6/24/2014
    6/25/2014
    6/26/2014
    6/27/2014

    I have the form setup to to prompt for the start and end dates. It's the VBA coding to do the dateadd function in a loop I'm trying to figure out.

  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,597
    Something like:

    Dim dteNew As Date
    dteNew = Me.tbxStart
    While dteNew < Me.tbxEnd
    CurrentDb.Execute "INSERT INTO tablename([datefield]) VALUES(#" & dteDate & "#)"
    dteNew = dteNew + 1
    Wend

    An alternative to the CurrentDb.Execute is to open a recordset object of the destination table and add records to the recordset.
    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
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    Thanks. I was able to figure it out. Here is the code I wrote, which included inserting the weekday name into another field.

    Private Sub AddDates_Click()
    Dim dbs As Database
    Dim strDayName As String
    Dim sDate As Date
    Dim eDate As Date
    sDate = StartDate
    eDate = EndDate


    Set dbs = CurrentDb
    NextDate = sDate


    For i = NextDate To eDate
    strDayName = WeekdayName(Weekday(NextDate))
    strsqlInsert = "Insert into ClassDaysTemp (TempDate, DayName) VALUES (#" & NextDate & "#, '" & strDayName & "')"
    dbs.Execute (strsqlInsert)
    NextDate = DateAdd("d", 1, NextDate)
    Next


    End Sub

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Another way to skin the cat:
    Code:
    Private Sub cmdGenerateRecords_Click()
    
    Dim i As Integer
    
    If Nz(Me.StartDate, "") = "" Or Nz(Me.EndDate, "") = "" Then
      MsgBox "You Must First Enter Start and End Dates!"
      Exit Sub
    End If
    
    i = DateDiff("d", Me.StartDate, Me.EndDate) + 1
    
    For i = 1 To DateDiff("d", Me.StartDate, Me.EndDate) + 1
       DoCmd.GoToRecord , , acNewRec
       Me.YourDateField = Me.StartDate + (i - 1)
      Next i
    
    End Sub

    Linq ;0)>

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

Similar Threads

  1. Replies: 14
    Last Post: 05-28-2014, 03:09 AM
  2. date range instead list of dates
    By wnicole in forum Reports
    Replies: 2
    Last Post: 11-28-2013, 03:43 AM
  3. Limit report to a range of dates
    By nevets in forum Reports
    Replies: 2
    Last Post: 03-03-2012, 07:13 PM
  4. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  5. Count Dates within Date Range
    By eckert1961 in forum Queries
    Replies: 2
    Last Post: 03-08-2009, 10:58 AM

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