Results 1 to 3 of 3
  1. #1
    TenOc is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    94

    Calculate a date 30 days ago

    I am trying to calculate a date 30 days ago. This is what I am using. I put it in a message box to see the results. The results is 8/27/2021. What am I doing wrong


    MsgBox Now - 30

    I have also tried MsgBox DateAdd("d", -30, Now) with the same 8/27/2021 results

    The full code is :



    Code:
    Private Sub Command177_Click()
    
    
    
    
        Dim sPath As String
        Dim objFSO As Object, objFolder As Object
        Dim objfile As Object
    
    
    
    
        '~~> Change as needed
        ' sPath = "C:\Users\Siddharth Rout\Desktop\Test\"
        
            sPath = "F:\NDC Documents\NDC Access\AutoBackup\"
            
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(sPath)
    
    
        For Each objfile In objFolder.Files
            If Format(objfile.DateLastModified, "MM-DD-YYYY") < Format(DateAdd("d", -30, [Now])) Then
                Kill objfile
                
                
            End If
        Next objfile
        MsgBox DateAdd("d", -30, Now)
        
    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I think you should use Date() instead of Now() as you are comparing a date with no time part with one with a time part by using Now().

    So your code should be:
    Code:
      If Format(objfile.DateLastModified, "MM-DD-YYYY") < Format(DateAdd("d", -30, Date)) Then
    Also note that the Format function returns a string so the comparison might not be what you expect. You should probably try to do it without using Format.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What date are you expecting, and what date is it now where you are? By my calculation, your result is correct.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 11-26-2013, 09:25 AM
  2. Replies: 1
    Last Post: 02-12-2013, 03:48 AM
  3. Replies: 7
    Last Post: 01-28-2013, 05:21 PM
  4. Replies: 1
    Last Post: 11-12-2010, 01:16 AM
  5. calculate no. of working days
    By JOSE LUIS in forum Access
    Replies: 1
    Last Post: 02-01-2010, 03:55 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