Solved

VBA Function to find a fiscal month start date

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

757 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