Page 3 of 3 FirstFirst 123
Results 31 to 41 of 41
  1. #31
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You may have read my post before I finished editing. Review again.
    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.

  2. #32
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    I am not sure what the DLookup expression should be.. How should I write it so it deletes the first record? I do understand how to write a DLookup just not in a way where it skips records

  3. #33
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    DLookup cannot delete record, it can only provide info. Actually, don't even need the DLookup. Just run delete action on the EffDate.

    Code:
     Dim rs As DAO.Recordset, intMons As Integer, x As Integer, dteDate As Date
     Set rs = CurrentDb.OpenRecordset("SELECT HSSN, EffDate, ToDate FROM tablename")
     With rs
         .MoveLast
         .MoveFirst
         While Not .EOF
             intMons = DateDiff("m", !EffDate, !ToDate) + 1
             dteDate = DateSerial(Year(!EffDate), Month(!EffDate), 1)
             For x = 1 To intMons
                 If x = 1 Then CurrentDb.Execute "DELETE FROM EmpHistory WHERE HSSN='" & !HSSN & "' AND NewDate=#" & dteDate & "#"
                 CurrentDb.Execute "INSERT INTO EmpHistory(HSSN, NewDate) VALUES('" & !HSSN & "', #" & dteDate & "#)"
                 dteDate = DateAdd("m", 1, dteDate)
             Next
             .MoveNext
         Wend
     End 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.

  4. #34
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    error: too few parameters. Expected 1.


    Does it have to do with my INSERT INTO command: "INSERT INTO EmployeeHistory_tbl(HSSN, HSTATUS, NewDate) VALUES('" & !HSSN & "', '" & !HSTATUS & "', #" & dteDate & "#)"

  5. #35
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Is HSTATUS a field retrieved by the Recordset? I would think not. So what status do you want in each of these records?

    "INSERT INTO EmployeeHistory_tbl(HSSN, HSTATUS, NewDate) VALUES('" & !HSSN & "', 'some value here', #" & 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.

  6. #36
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    Quote Originally Posted by June7 View Post
    Is HSTATUS a field retrieved by the Recordset? I would think not. So what status do you want in each of these records?

    "INSERT INTO EmployeeHistory_tbl(HSSN, HSTATUS, NewDate) VALUES('" & !HSSN & "', 'some value here', #" & dteDate & "#)"
    I have that with !HSTATUS.

    The debug is highlighting, as the error, CurrentDb.Execute "DELETE FROM EmpHistory WHERE HSSN='" & !HSSN & "' AND NewDate=#" & dteDate & "#"

    Code:
    Sub MonthlyEmployeeRS()
    
    Dim rs As DAO.Recordset, intMons As Integer, x As Integer, dteDate As Date
    Set rs = CurrentDb.OpenRecordset("SELECT HSSN, EffDate, ToDate, HSTATUS FROM EmpHistory")
    With rs
        .MoveLast
        .MoveFirst
        While Not .EOF
            intMons = DateDiff("m", !EffDate, IIf(!ToDate > Date, Date, !ToDate)) + 1
            dteDate = DateSerial(Year(!EffDate), Month(!EffDate), 1)
            For x = 1 To intMons
                If x = 1 Then CurrentDb.Execute "DELETE FROM EmpHistory WHERE HSSN='" & !HSSN & "' AND NewDate=#" & dteDate & "#"
                CurrentDb.Execute "INSERT INTO EmployeeHistory_tbl(HSSN, HSTATUS, NewDate) VALUES('" & !HSSN & "', '" & !HSTATUS & "', #" & dteDate & "#)"
                dteDate = DateAdd("m", 1, dteDate)
            Next
            .MoveNext
        Wend
    End With
    
    
    
    End Sub


  7. #37
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You have two tables: EmpHistory, EmployeeHistory_tbl?

    EmpHistory is source for recordset? EmpHistory has HSTATUS field?

    EmployeeHistory_tbl is the table to insert new records? Then EmployeeHistory_tbl is also table for DELETE.

    In spite of the table names in my example, use the appropriate table names from your db.
    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.

  8. #38
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    yes that is all correct. The record source is EmpHistory and EmployeeHistory_tbl is where the data is being output to. EmpHistory has the status field along with the rest of the data.

  9. #39
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    But your code shows the DELETE acting on EmpHistory, not EmployeeHistory_tbl.
    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.

  10. #40
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    oh.. well that makes sense

  11. #41
    kwooten is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    255
    Quote Originally Posted by June7 View Post
    But your code shows the DELETE acting on EmpHistory, not EmployeeHistory_tbl.
    that fixed it.. thank you so much.. I'd buy you a beer if I could

Page 3 of 3 FirstFirst 123
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