+ Reply to Thread
Results 1 to 8 of 8

Populating 10+ Years of Dates Table

Hybrid View

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

    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.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

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

    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 the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

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

    Re: Populating 10+ Years of Dates Table

    Thanks Thomas,

    I am not used to VBA for Access syntax but I am about to learn.

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

    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.

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

    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!

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

    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.

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

    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.

  8. #8
    Valued Forum Contributor mahju's Avatar
    Join Date
    11-27-2010
    Location
    Pakistan, Faisalabad
    MS-Off Ver
    Excel 2010 plus
    Posts
    730

    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.
    Mark the thread as solved if you are satisfied with the answer.


    In your first post under the thread tools.

    Mahju

+ 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.6.0 RC 1