Everyone, my VBScript is timing out () when I test a improvement to Red's 6/4/03 Recurring Events Calendar Mod. I am trying to modify his code to allow choices like the 1st Monday of every month, or 4th Thursday of every month, or Last Wednesday of every month, etc. I have created options 18-52 in cal_post.asp which are as follows:
18 = 1st Mon
19 = 1st Tue
...
24 = 1st Sun
25 = 2nd Mon
...
45 = 4th Sun
46 = Last Mon
...
52 = Last Sun
I am now trying to design efficient code to calculate the appropriate dates. Instead of adding 35 more select statements, I am trying to be intelligent about it and only add 7 more, using the Events_Repeat variable as an indicator of which version of each weekday I want, then adding that many weeks to the first one found in each month. Naturally, this assumes I have found a way to find the 1st day of the following month. The "Last" cases are a little different, so I add an extra month to the 1st day of the month then subtract a week to find the last week of the month. The code I've written to add the above into cal_post_info1.asp and cal_post_info2.asp is as follows:
Case 18, 25, 32, 39, 46 '1st,2nd,3rd,4th,Last Mon (Monthly)
' step - find the 1st of the next month
dateCursor = DateAdd("d", -(1 - (DatePart("d",dateCursor))), dateCursor) 'backup to 1st of this month
dateCursor = DateAdd("m", 1, dateCursor)
if Event_Repeat = 46 then 'find the LAST one
dateCursor = DateAdd("m", 1, dateCursor) 'Jump one month forward
dateCursor = DateAdd("ww", -1, dateCursor) 'jump one week backward - now on last week of month
end if
'to compensate for DO loop adding one day before testing result
'which is no longer appropriate since I already changed dateCursor above
dateCursor = DateAdd("d", -1, dateCursor)
Do
dateCursor = DateAdd("d", 1, dateCursor)
Loop Until Weekday(dateCursor) = vbMonday
if Event_Repeat <> 18 and Event_Repeat <> 46 then
dateCursor = DateAdd("ww", (Event_Repeat - 18) / 7, dateCursor)
end if
Note that I have not added any more loops other than what is in previous options, nor have I changed the exit loop criteria, so I am not sure why I end up in an infinite loop, if in fact that's what I'm in.
Can anybody see what I'm doing wrong? I'd love to get this working and post the required changes for everyone... Thanks in advance.
(edited for typos)