+ Reply to Thread
Results 1 to 8 of 8

Thread: Populating 10+ Years of Dates Table

  1. #1
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,264

    Populating 10+ Years of Dates Table

    Hi all,

    I have created a Dates table in Acces 2003 that has the following attributes:
    DateID (PK), Days, Months, Quarters, & Years. I was wondering if there was a way to populate this table, which will span 10+ years, without manually doing it? While I have worked with Access in the past, I have yet to create a table like this. Any help is, as always, appreciated.

    -Michael
    Last edited by Mordred; 04-28-2011 at 12:38 PM.
    Please leave a message after the beep!

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010
    Posts
    856

    Re: Populating 10+ Years of Dates Table

    You'd need to do some looping to get the below to work, and add variables for the values you want, and it's untested...
    Private Sub CmdAddRecord_Click()
    On Error GoTo Err_CmdAddRecord_Click
     
        DoCmd.GoToRecord , , acNewRec
        'Prevent user warnings
      DoCmd.SetWarnings False
     
    Dim SQL As String
       SQL = "INSERT INTO tbl_Dates values(Variables here for the values to insert put in order of your fields in the table except for DateID which probably autopopulates);"
     
        DoCmd.RunSQL SQL
     
    'Allow user warnings
      DoCmd.SetWarnings True
     
    Exit_CmdAddRecord_Click:
        Exit Sub
     
    Err_CmdAddRecord_Click:
        MsgBox Err.Description
        Resume Exit_CmdAddRecord_Click
     
    End Sub
    If your question has been satisfactorily addressed, please consider marking it solved. Click here to see how.

    Also, you might want to add to the user's reputation by clicking the scales icon - it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,264

    Re: Populating 10+ Years of Dates Table

    Thanks Thomas,

    I am not used to VBA for Access syntax but I am about to learn.
    Please leave a message after the beep!

  4. #4
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,264

    Re: Populating 10+ Years of Dates Table

    Sorry for my ignorance Thomas but I have no idea how that code is supposed to populate the fields that I have. I guess I could create this in Excel and then import the table, unless you have a lot of patience to walk me through your code.
    Please leave a message after the beep!

  5. #5
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010
    Posts
    856

    Re: Populating 10+ Years of Dates Table

    That might be a better option. The code is untested and it would take quite a bit for me to get it ready to run.

    The first thing you'd need to do is find a way to increment the date, month and qtr values, and a for next with a counter could do that for you. This line:
    SQL = "INSERT INTO tbl_Dates values(
    is where you'd position values separated with commas for each field you'd be writing to.

    Excel is probably going to be a faster bet since I'm a bit of a noob to Access - I've just done this before but it's been quite a while...sorry!
    If your question has been satisfactorily addressed, please consider marking it solved. Click here to see how.

    Also, you might want to add to the user's reputation by clicking the scales icon - it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  6. #6
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,264

    Re: Populating 10+ Years of Dates Table

    No need to apologize, I also posted a new thread here but it is waiting for mod approval I guess because it is not up on the forum yet.
    Please leave a message after the beep!

  7. #7
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,264

    Re: Populating 10+ Years of Dates Table

    I did this by importing the table from Excel but I do plan on figuring this out by coding it.
    Please leave a message after the beep!

  8. #8
    Valued Forum Contributor mahju's Avatar
    Join Date
    11-27-2010
    Location
    Pakistan, Faisalabad
    MS-Off Ver
    Excel 2003, Excel 2007 (Portable)
    Posts
    388

    Re: Populating 10+ Years of Dates Table

    Hi

    I am want to add something

    See my solution

    I have used two databases. One for data (data.mdb) and the other for code (code.mdb). run the form from code.mdb and it will populate the data in data.mdb

    Hera is the code under Generate button

    Private Sub Command1_Click()
    Dim strdate As Date
    Dim dbs As DAO.Database
    Dim recinsert As DAO.Recordset
    
    strpath = CurrentProject.Path & "\data.mdb"
    Set dbs = OpenDatabase(strpath)
    strinsert = "select * from datatable"
    Set recinsert = dbs.OpenRecordset(strinsert)
    reccount = recinsert.RecordCount
    'Use the line below to delte the already populated records (Remove comment mark (')
    If reccount > 0 Then
    recinsert.Delete
    End If
    strdate = Me.Text2.Value
    stryear = Me.Text5.Value
    looptime = stryear * 365
    For i = 1 To looptime
    recinsert.AddNew
    recinsert("dateid") = strdate
    recinsert.Update
    strdate = strdate + 1
    Next i
    Set recinset = Nothing
    Set dbs = Nothing
    MsgBox "Data population completed"
    
    End Sub
    Unzip the attached zip file and run the code.mdb in data population folder.

    Thanks

    Best Regards
    Attached Files Attached Files
    Last edited by mahju; 05-01-2011 at 06:06 AM.
    Mahju

    Mark the thread as solved if you are satisfied with the answer

    Rule 9
    Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0