Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255

    one record into multiple records based on from and to date

    If I have a record that has a effective date of 1/1/2015 and expire date of 5/30/2015 then is it possible to get a record for each month based on these dates; so this 1 record would yield 5 records, one for each month active?

  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,641
    With VBA code that creates new records into table.
    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
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    I am VBA illiterate... would you mind helping?

    I have product number, effective date and to date.. can it be done off just these 3 pieces?

  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,641
    Something like:
    Code:
     Dim intMons As Integer, x As Integer, dteDate As Date
        intMons = DateDiff("m", Me.tbxEffectiveDate, Me.tbxExpirationDate) + 1
        dteDate = DateSerial(Year(Me.tbxEffectiveDate), Month(Me.tbxEffectiveDate), 1)
        For x = 1 To intMons
            CurrentDb.Execute "INSERT INTO sometable(ProductNum, NewDate) VALUES(" & Me.tbxProductNum & ", #" & dteDate & "#)"
            dteDate = DateAdd("m", 1, dteDate)
        Next
    Advise no spaces or special characters/punctuation (underscore is exception) in naming convention.
    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
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    maybe I didnt create this correctly but when I hit run a Macros box opens up. I went to Create Module then copied your code in and changed to fit my tables.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    I revised my code significantly so it just works with values from a form.

    You probably want this code behind a form in button Click event.

    Create a button then select [Event Procedure] in its Click event property, click the ellipsis (...) to open VBA editor. Type code in the procedure.
    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.

  7. #7
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    getting an error... Syntax error in INSERT INTO statement... will this code create the table for me or do I need to create it first?

    CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, YrMo) VALUES(" & rs!HSSN & ", 3" & DateAdd("m", 1, dteDate)

  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,641
    Table must already exist.

    Are you sticking with the Recordset code? The line you posted shows some errors I had not yet fixed.

    CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, YrMo) VALUES(" & rs!HSSN & ", #" & DateAdd("m", 1, dteDate) & "#"

    Post your actual entire procedure if need more help.

    Is this a process that you will have to do periodically or is this a one-time only occurrence?
    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.

  9. #9
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    i updated based on the new code above and get Compile error: Method or data member not found.. Is this right?

    Code:
     Dim intMons As Integer, x As Integer, dteDate As Date
        intMons = DateDiff("m", Me.tbxEFFDATE, Me.tbxEFFDATE) + 1
        dteDate = DateSerial(Year(Me.tbxEFFDATE), Month(Me.tbxEFFDATE), 1)
        For x = 1 To intMons
            CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, NewDate) VALUES(" & Me.tbxHSSN & ", #" & dteDate & "#)"
            dteDate = DateAdd("m", 1, dteDate)
        Next

  10. #10
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    Quote Originally Posted by June7 View Post
    Table must already exist.

    Are you sticking with the Recordset code? The line you posted shows some errors I had not yet fixed.

    CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, YrMo) VALUES(" & rs!HSSN & ", #" & DateAdd("m", 1, dteDate) & "#"

    Post your actual entire procedure if need more help.

    Is this a process that you will have to do periodically or is this a one-time only occurrence?
    would probably do once a month.. should I go back to the other code?

  11. #11
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    correction...

    Code:
    Private Sub Command0_Click()
    
     Dim intMons As Integer, x As Integer, dteDate As Date
        intMons = DateDiff("m", Me.tbxEFFDATE, Me.tbxTODATE) + 1
        dteDate = DateSerial(Year(Me.tbxEFFDATE), Month(Me.tbxEFFDATE), 1)
        For x = 1 To intMons
            CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, NewDate) VALUES(" & Me.tbxHSSN & ", #" & dteDate & "#)"
            dteDate = DateAdd("m", 1, dteDate)
        Next
    
    
    End Sub

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    The DateDiff() calculation is wrong. It needs to reference two date values, not the same one twice. Do some research on use of this function.

    The compiler is probably not finding a textbox. Make sure names are correctly spelled.

    Is HSSN a text or number type field? If it is text, will need apostrophe delimiters.

    VALUES('" & Me.tbxHSSN & "', #" & dteDate & "#)"
    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.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    If you want to act on multiple records then yes, use Recordset version. However, will need to apply filter criteria in the recordset so don't repeat this process for prior month records.
    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.

  14. #14
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    it is highlighting "Private Sub Command0_Click()"

  15. #15
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    Quote Originally Posted by June7 View Post
    If you want to act on multiple records then yes, use Recordset version. However, will need to apply filter criteria in the recordset so don't repeat this process for prior month records.
    Can you repost the recordset code? I will run for all the data I have currently then apply criteria to run only prior month going forward.. yes HSSN is text.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Updating Multiple Record Form Based on Record Count
    By William McKinley in forum Forms
    Replies: 2
    Last Post: 12-31-2014, 12:45 PM
  2. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  3. Showing records based on certain date
    By mindbender in forum Queries
    Replies: 6
    Last Post: 05-06-2013, 04:27 PM
  4. Exporting records based on date
    By justinwright in forum Import/Export Data
    Replies: 35
    Last Post: 05-11-2011, 04:53 PM
  5. Replies: 1
    Last Post: 12-10-2009, 08:41 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