steinmetzw42 via AccessMonster.com said:
I'm trying to created a user defined function in the VBA editor. I
need this function to be called in a query to find due dates
depending on the frequency that that specific number is equal to.
eg.. Days = 5 and Frequency =BD then I need it to calculate 5
workdays from yesterdays date (because this when the money would have
dropped, and it would show on the report today) but if it Days = 18
and Frequency = CD then I need it to bring back the next up comming
18th day of the month (such as 3/18/07). Let me know if you need
more details.. Thanks.
Ah. I'd thought the code was going to be in the module of a report or
form. If you're going to be putting it in a standard module, in a
function intended to be called from a query, it's best to avoid adding
the overhead of a call to DLookup if you can. If I were you, I'd use
the query itself to join tbl_RemitDays, and then pass the fields
necessary to calculate the due date from the query to the function. So
your query would be something loosely along the lines of this:
SELECT
<existing list of fields>,
fncDueDate(tbl_RemitDays.Frequency, tbl_RemitDays.Days)
As DueDate
FROM
<some table>
LEFT JOIN
tbl_RemitDays
ON <some table>.Number = tbl_RemitDays.Number
Then your function, which I have prospectively named "fncDueDate", would
be declared something like this:
'----- start of example code -----
Function fncDueDate( pFrequency As Variant, pDays As Variant) _
As Variant
If IsNull(pFrequency) Or IsNull(pDays) Then
fncDueDate = Null
Else
' Depending on the frequency specified, compute
' and return the appropriate due date.
Select Case pFrequency
Case "BD"
fncDueDate = BDFunk(pFrequency, pDays)
Case "CD"
fncDueDate = CDFunk(pFrequency, pDays)
Case "Daily"
fncDueDate = Daily()
Case "FCD"
fncDueDate = FCDFunk(pFrequency, pDays)
Case Else
fncDueDate = Null ' or else return an error
End Select
End If
End Function
'----- end of example code -----