Nevermind, I put it into the query and it worked perfectly. Thanks a bunch!
> This seems to work, but I keep getting a "Run Time error: Overflow" when I
> run it. Also, where should I put the iif statement? Before or after Do?
[quoted text clipped - 19 lines]
> > >
> > > Thanks
Gen,
The function code does not check for a valid date and a valid weekly
interval, therefore you must check for it; that is the purpose of the IIf
statement, which is not meant to be placed in the function code itself but
rather as an expression for a calculated field in a query or as the Control
Source of a form control.
Otherwise, if you're doing data entry and enter a starting date, with the
interval blank or 0, the function will loop continually because the function
value never exceeds the current date. Eventually, Access runs out of memory
and triggers the overflow error.
Set a calculated query field as follows, then set a textbox' ControlSource
to the name of the field (NextDeliveryDate):
NextDeliveryDate: IIf((Nz([StartingDate])=0 Or
Nz([WeeklyInterval])=0),Null,NextDeliveryDate([StartingDate],[WeeklyInterval]))
OR
set the textbox ControlSource directly to the value of the expression:
=IIf((Nz([StartingDate])=0 Or
Nz([WeeklyInterval])=0),Null,NextDeliveryDate([StartingDate],[WeeklyInterval]))
I've added basic error-checking to the function code below. I also realized
that if you enter a future starting date, it will return one interval past
the starting date as the next shipment. If you'd prefer it return the
starting date, include the optional
If dteStart > dteToday ..End If block, otherwise, delete it:
Public Function NextDeliveryDate(dteStart As Date, intInterval As Integer)
As Date
On Error GoTo Err_Handler
' Note: Function does not check for valid date and integer parameters
Dim i As Integer
Dim dteToday As Date
i = 0
dteToday = Date
' Start of optional block to test for future starting date
If dteStart > dteToday Then
dteStart = dteStart - intInterval * 7
End If
' End of optional block
Do
i = i + 1
NextDeliveryDate = dteStart + i * intInterval * 7
Loop Until NextDeliveryDate >= dteToday
Err_Exit:
Exit Function
Err_Handler:
MsgBox Err.Number & vbCrLf & Err.Description
Resume Err_Exit
End Function
Hope that helps.
Sprinks
> This seems to work, but I keep getting a "Run Time error: Overflow" when I
> run it. Also, where should I put the iif statement? Before or after Do?
[quoted text clipped - 19 lines]
> > >
> > > Thanks