Solved

VBA Function to find a fiscal month start date

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL - Cumulating field values to one field 5 19
Exporting Access Tables as CSV 3 23
Access Open Report with SQL Parameter 11 29
Help with DoEvents 8 25
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 When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

813 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

18 Experts available now in Live!

Get 1:1 Help Now