Solved

VBA Function to find a fiscal month start date

Posted on 2013-12-04
2
751 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 119

Accepted Solution

by:
Rey Obrero 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now