Solved

VBA Function to find a fiscal month start date

Posted on 2013-12-04
2
761 Views
Last Modified: 2013-12-05
Hi Experts,

Our fiscal year starts in November and ends in September.  I have built a function that finds the latest fiscal November start month in the data.  (i.e. if the data had 2013/11/30 and 2012/11/30, 2013/11/30 would be returned)  I've finally got it so it doesn't show errors and when I press F8 the variables show the correct date, however, the problem is that when I try to use the function in a query to filter the date by the function, I'm not getting any results at all.  Any ideas on how to fix my function?

Thanks!


Option Compare Database
Option Explicit

Public Function MaxDataDate()
Dim mDataDate As Date
mDataDate = DLookup("MaxOfDataDate", "q_DataDate_MAX")
MaxDataDate = mDataDate
End Function

Public Function MinFiscalYearMonth() As Date
Dim fDataDate As Date
Dim fYear As Integer
Dim vdate As Variant
Dim testNovember As Date
Dim testNovember2 As Date

fYear = Year(MaxDataDate())
vdate = fYear & "/11/30"

testNovember = DateValue(vdate)
testNovember2 = DateAdd("yyyy", -1, testNovember)

If DCount("[datadate]", "t_OD_Database_Portfolio_Level", "[datadate] = " & CDate(testNovember)) > 0 Then

    MinFiscalYearMonth = testNovember
   
    Else: MinFiscalYearMonth = testNovember2
End If

End Function
0
Comment
Question by:grmcra
2 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39697194
try this

If DCount("[datadate]", "t_OD_Database_Portfolio_Level", "[datadate] = #" &  testNovember & "#") > 0 Then


or revised the function


Public Function MinFiscalYearMonth() As Date
Dim fDataDate As Date
Dim fYear As Integer
Dim vdate As date
Dim testNovember As Date
Dim testNovember2 As Date

fYear = Year(MaxDataDate())
vdate =dateserial(Year(MaxDataDate()),11,30)

testNovember = vdate
testNovember2 = DateAdd("yyyy", -1, testNovember)

If DCount("[datadate]", "t_OD_Database_Portfolio_Level", "[datadate] = #" &  testNovember & "#") > 0 Then

    MinFiscalYearMonth = testNovember
   
    Else: MinFiscalYearMonth = testNovember2
End If

End Function
0
 
LVL 84
ID: 39697196
Can you show how you're doing this:

when I try to use the function in a query to filter the date by the function
Generally, you'd use this in the Query itself, and then filter the columns, like this:

SELECT Col1, Col2, MinFiscalYearMonth AS Col3 FROM SomeTable WHERE MinFiscalYearMonth>#11=2013#
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question