Results 1 to 5 of 5
  1. #1
    wlkr.jk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    20

    Running macro once per year

    Hi,

    I'm trying to run a macro that runs an archive command once per year. But, I need to make sure that the archive actually happens after a certain date even if the program isn't opened on that specific date to run the macro. For example, I need to archive all my records on March 31 every year. But if March 31 happens on a Saturday, no one is going to open the program, so the macro won't run. And the next time the program will be opened is the following Monday. Therefore, I've come up with the following code with the issue of somehow using an "exists()" function in access:

    Code:
    if exists(archivecount)=false then
    archivecount = 0
    end temp2 = DateDiff(yyyy,#31/03/2014#,Now()) if archivecount < temp2 then
    archive() archivecount = temp2
    end
    The reason for the first if statement is because I need to initialize the variable 'archivecount' if it hasn't already been set. I don't want to reinitialize the variable every time the code is run because I want to run the code every time the program opens and because my infinite archivecount is what determines if the actual archive() command should be run.



    For example, in 2016, the difference between 2016 and 2014 is obviously 2 years and if archivecount is less than 2, then that means the archive() function should be run....

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You would need a table to store the last time run or the NEXT time run, because the computer or app could be off.
    if Now > 1 year and hasnt been run yet for that year

  3. #3
    wlkr.jk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    20
    Ok thats what my next step would have been. I think what I'm going to do is just use a blank table with only the 'ID' field. Every time I do the 'archive()' command, I will add a new record to the table (therefore increase the number of records and effectively the largest ID number in the table being the number of times archive has been run).

    However, now I'm trying to figure out how to return the last ID number from the table. Any ideas?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Make a query or txtbox to call: DMAX([ID])

  5. #5
    wlkr.jk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    20
    Ok, so I created a new table tblArchiveCount. Currently, I have 1 record in the table to act as my first year

    Function AutoExec()
    On Error GoTo FY_End_Archive_Err
    With CodeContextObject
    Dim temp2 As Variant
    Dim archivecount As Variant
    temp2 = DateDiff(yyyy, #3/31/2014#, Now())
    archivecount = DMax("ID", "tblArchiveCount", "")
    If ((archivecount - 1) < temp2) Then
    Call Archive
    Call Add_Record

    End If
    End With


    FY_End_Archive_Exit:
    Exit Function


    FY_End_Archive_Err:
    MsgBox Error$
    Resume FY_End_Archive_Exit
    End Function


    So two questions, do you think this will do it, and what I'm I doing wrong as far as AutoExec() goes? I did it this way so that it would open automatically when I first open the program but nothing happens when I open it up (I put message boxes in for testing but took them out to post here)?

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

Similar Threads

  1. Replies: 3
    Last Post: 06-15-2013, 08:05 PM
  2. macro running in order
    By dumbledown in forum Access
    Replies: 2
    Last Post: 09-03-2012, 08:05 AM
  3. running macro in another DB
    By AdrianoG87 in forum Import/Export Data
    Replies: 1
    Last Post: 09-30-2011, 12:31 AM
  4. Running module from Macro
    By Harley Guy in forum Modules
    Replies: 6
    Last Post: 10-27-2010, 11:05 AM
  5. Running a VB function in a Macro
    By JuuJuu in forum Access
    Replies: 1
    Last Post: 10-27-2009, 02:50 AM

Tags for this Thread

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