Auto create record based on date and criteria in another record (1 Viewer)

home70

Registered User.
Local time
Yesterday, 21:53
Joined
Jul 10, 2006
Messages
72
I have a db where each record is a certain task to be done. Each record includes the date the task should be completed. Some of the tasks are one time only but some are recurring. I have been having to create a new record for every task even the recurring ones, but I have decided to try to automate creation of the recurring tasks.

I'd like to have it so that when I set up a task I can select from a drop down box that this task will not recur or that it will recur every week, month, etc, and (here's the part I can't figure out) then based on the selection it would create a new record of the same task but with the new due date based on which one of the recurrence options was selected.

For example, if the original task was:
Fax weekly expense report
Due 9/26/07
Repeat: weekly

I'd like for it to, on 9/26 or 9/27, automatically create this record
Fax weekly expense report
Due 10/3/07
Repeat: weekly

I don't want the same record to be changed to show the new date; I need to have a record for each task.
Thank you for your help.
 

MarkK

bit cruncher
Local time
Yesterday, 19:53
Joined
Mar 17, 2004
Messages
8,181
I'd steer away from having it create the new record a week at a time. If you're away sick from tuesday to thursday and you never open your program your wednesday recurring task never gets advanced. You see? What event triggers the creation of the new recurrance?
Rather, I'd write a recurring appointment wizard into which you can specify the period of repetition and the number of iterations. An example of a routine that could create a periodic recurrance using a number of days...
Code:
Sub CreateDailyRecurrance(FromTaskID as long, DayPeriod as long, i as long)
  dim rst as dao.recordset
  dim dte as date
  dim j as long
  set rst = currentdb.openrecordset( _
    "SELECT Caption, Date FROM tTask WHERE TaskID = " & FromTaskID)
  with rst
    dte = !Date
    for j = 1 to i
      dte = dte + DayPeriod
      currentdb.execute _
        "INSERT INTO tTask ( Caption, Date ) " & _
        "SELECT '" & !Caption & "', #" & dte & "#"
    next j
    .close
  end with
end sub
 

home70

Registered User.
Local time
Yesterday, 21:53
Joined
Jul 10, 2006
Messages
72
Thanks. I do see what you mean and had thought of the fact that if I didn't open the db when a record was set to be created then it wouldn't be created until I did open the db (I had planned on running the code to generate the needed records on open of the db's main form). In my case however I didn't think it much mattered since any records that needed to be created, and that weren't as a result of me not opening the db, would be created as soon as did open the db and therefore would be there waiting on me when I did enter the db after being away. Is this not correct? (I'm far from an Access guru, in fact I have to work pretty hard to make things work, haha)
Anyway, I do like your idea too, although it seems a bit further above my head than I was planning on going. In order to understand your code I'd ask you to show me the table structure that would apply to the code.
Thanks again for your help.
 

MarkK

bit cruncher
Local time
Yesterday, 19:53
Joined
Mar 17, 2004
Messages
8,181
The table structure is whatever your task table is. Each task needs an ID, a caption or a name, a date, and maybe you have other fields that the recurring item will need copied from the original.
Replace my field and table names with what you have.
But don't let me stop you either. If you have a vision, pursue it. What I'll do is comment my code so you can get a better idea what it does...
Code:
Sub CreateDailyRecurrance(FromTaskID as long, DayPeriod as long, i as long)
  dim rst as dao.recordset
  dim dte as date
  dim j as long
[COLOR="Green"]  'open a recordset of the single, original task that will recurr[/COLOR]
  set rst = currentdb.openrecordset( _
    "SELECT Caption, Date FROM tTask WHERE TaskID = " & FromTaskID)
[COLOR="Green"]  'using that recordset...[/COLOR]
  with rst
[COLOR="green"]    'retrieve the date of the original item[/COLOR]
    dte = !Date
[COLOR="green"]    'loop for the number of recurrances you want[/COLOR]
    for j = 1 to i
[COLOR="green"]      'for each recurrance, add the period, in days, to the original date
      'for weekly recurrance DayPeriod will = 7, and dte will be 7 days later in each loop[/COLOR]
      dte = dte + DayPeriod
[COLOR="green"]      'insert the new task with the new date back into the tasks table[/COLOR]
      currentdb.execute _
        "INSERT INTO tTask ( Caption, Date ) " & _
        "SELECT '" & !Caption & "', #" & dte & "#"
    next j   
    .close [COLOR="green"]'rst[/COLOR]
  end with
end sub
...and then you can just pick what way you think is cooler.
 

Users who are viewing this thread

Top Bottom