473,806 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coding a function to return a value

I have a query that pulls data for the month, using the Month End Date
from a form (user enters) as criteria. It works. I want to use the
same query to pull "month to date" data, on a weekly basis. So, for
week 1, it would only show the first 7 days; week 2 the first 14 days,
etc.

If I enter the next month ending date (1/1/05) right now, I get the
information for days since the last month end until current date.
That's perfect. My problem is that I would like the system to figure
out the Month End Date from the user entering Week End Date and feed
Month End Date to this query for the different report. All the data I
need is in the query and it works if I manually enter the month ending
date, but I don't know how to get the code to do it.

I wrote a select query to give me all "associated " dates for one sales
date - Last Year, Month Start and Ending Dates, etc. (We are on a
unique fiscal calendar, so I have my dates in a table). I want to
have the code "get" the Month Ending date and feed it to the query for
this report when the user enters "week endind date" and chooses to run
the weekly reports.

This is what I tried. I just don't understand functions too well.

Function GetMonthEndDate (MonthEndDate As Date)

' Run query to get MonthEndDate for WeekEndingDate entered (for build
report)

DoCmd.OpenQuery "qryGetDatesfor SalesDate", acNormal, acReadOnly
MsgBox "New " & MonthEndDate

End Function

I know I'm in the code because I see the query opened and I get my
message box, but it just says "new".

To run the function, I say:
' Run query to get MonthEndDate for WeekEndingDate entered (for Spags
build report)
GetMonthEndDate (MonthEndDate)
MsgBox "in 2 Code " & MonthEndDate

I get this message box, but it just says "In 2 Code".

Thanks so much.
Sara
Nov 13 '05 #1
13 2164

"Sara" <sa*******@yaho o.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
... My problem is that I would like the system to figure
out the Month End Date from the user entering Week End Date and feed
Month End Date to this query for the different report.


Create the following function and pass the week end date to it.

Public Function MonthEnd(dt As Date)
'Show the last day of month for a given date

MonthEnd = DateSerial(Year (dt), Month(dt) + 1, 0)

End Function

hth,

Tim Mills-Groninger
Nov 13 '05 #2
Tim -
Thanks, but the problem still exists. Perhaps I wasn't clear:

Problem 1: We have a unique fiscal calendar. All months end on
Saturday and start on Sunday. Some are 5 weeks and some are 4 weeks
long. For, example, this year, December, our 11th month, ends on
1/1/05. That is why I have a separate table, called "AllDates" that
gives me today, last year's same day, week end, month end, fiscal month
number, etc. I need to take the date the user enters and find the month
ending date for that date. If the user enters 11/5/04, I would see
11/27/04, for example.

Problem 2: I put your code in and added the code:
Dim dt As Date

Call MonthEnd(dt)
I added "Msgbox dt" at the end of your function code to see what I was
getting back.

I hope that is right.

The problem is that I got back 12:00:00 am. I am pretty sure it
wouldn't have been able to know my fiscal calendar without more code or
a query or table reference (which is my origianl question, really), but
I'm using this to try to learn how to call a function and I clearly have
something wrong.

Can you help?

Thanks - in advance.
sara

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
sara <an********@dev dex.com> wrote in message news:<110218345 8.6NhiR9PM+7bLt XRfOtRDkA@teran ews>...
Tim -
Thanks, but the problem still exists. Perhaps I wasn't clear:

Problem 1: We have a unique fiscal calendar. All months end on
Saturday and start on Sunday. Some are 5 weeks and some are 4 weeks
long. For, example, this year, December, our 11th month, ends on
1/1/05. That is why I have a separate table, called "AllDates" that
gives me today, last year's same day, week end, month end, fiscal month
number, etc. I need to take the date the user enters and find the month
ending date for that date. If the user enters 11/5/04, I would see
11/27/04, for example.


The following function assumes that having three or less days in the
final week puts that week into the next fiscal month:

Public Function LastDayOfFiscal Month(dt As Date) As Date
Dim int5thSat As Integer
Dim int1stSat As Integer
Dim intMonthEnd As Integer
Dim dtNM As Date

int1stSat = (6 - WeekDay(DateSer ial(Year(dt), Month(dt), 1), 2)) Mod 7
+ 1
If int1stSat < 4 And Day(dt) <= int1stSat Then
'Belongs to previous month
LastDayOfFiscal Month = DateSerial(Year (dt), Month(dt), int1stSat)
Exit Function
End If
int5thSat = (6 - WeekDay(DateSer ial(Year(dt), Month(dt), 1), 2)) Mod 7
+ 29
intMonthEnd = Day(DateSerial( Year(dt), Month(dt) + 1, 0))
dtNM = DateAdd("m", 1, dt)
If int5thSat <= intMonthEnd Then
If Day(dt) <= int5thSat Then
LastDayOfFiscal Month = DateSerial(Year (dt), Month(dt), int5thSat)
Else
LastDayOfFiscal Month = LastDayOfFiscal Month(DateAdd(" d", 7, dt))
End If
Else
'Last Saturday is the fourth Saturday. Check remaining days to see
which way week goes.
If intMonthEnd - int5thSat + 7 < 4 Then 'Three or less extra days
go to next fiscal month
If Day(dt) <= int5thSat - 7 Then
LastDayOfFiscal Month = DateSerial(Year (dt), Month(dt), int5thSat
- 7)
Else
LastDayOfFiscal Month = LastDayOfFiscal Month(DateAdd(" d", 7, dt))
End If
Else
'Four or more extra days force the fiscal month to end a week
later
LastDayOfFiscal Month = DateSerial(Year (dtNM), Month(dtNM),
int5thSat - intMonthEnd)
End If
End If
End Function

Watch out for the linewrap. I used the expression given by Tim for
the month end. I did some moderate testing, but be sure to test this
function to your satisfaction before using it. If I guessed wrong
about how you split up your fiscal months post back.

James A. Fortune
Nov 13 '05 #4
James -
I certainly don't mean to seem ungrateful, but there are still 2
problems.

1. I put your code in, along with Msgboxes to display the variable
"dt", which gave 12:00:00 AM all the time. Then I added the display of
dtNM, which gave 1/30/1900 in the function and nothing after the call to
the function and return from the call. I don't know what's wrong there.

2. The second thing, and probably more important (the first is, I
think, my understandig of coding and displaying variables), is that I
think you have the Fiscal calendar wrong. It can't be calculated. It
is calculated then decided each year by Accounting and the Chairman.
(every now and then we have a 53rd week and we have to plan where that
goes) Generally, the months (there are only 12 in a year) go 4-5-4 (4
weeks, 5 weeks, 4 weeks).

So, if I understand your code correctly, you are determining the end of
the month based on how many days in the calendar month after the last
Saturday. I think that works for 2004 and even 2003, but I am not sure
it is ALWAYS going to be the case, or what will happen when we have a
53rd week. This is why we have a lookup table that holds ALL our dates
for all weeks for current and prior years.

Also, I am wondering about the DLOOKUP function I've seen on posts and
in Help. Would that (somehow) work? I am thinking that I will find
occasion to use other dates in one row for other functions. (Such as
quarter start and end dates - again,using our fiscal quarters; and Last
year's quarter start and end). I was hoping for a simple solution and
am not sure there is one!

Again, I am grateful for the interest and help; we're just not there
yet.

Thanks - in advance.
sara

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5
rkc

"sara" <an********@dev dex.com> wrote in message
news:1102257861 .4KiiT9eM69p7Ib gkYA6Cdw@terane ws...
So, if I understand your code correctly, you are determining the end of
the month based on how many days in the calendar month after the last
Saturday. I think that works for 2004 and even 2003, but I am not sure
it is ALWAYS going to be the case, or what will happen when we have a
53rd week. This is why we have a lookup table that holds ALL our dates
for all weeks for current and prior years.

Also, I am wondering about the DLOOKUP function I've seen on posts and
in Help. Would that (somehow) work? I am thinking that I will find
occasion to use other dates in one row for other functions. (Such as
quarter start and end dates - again,using our fiscal quarters; and Last
year's quarter start and end). I was hoping for a simple solution and
am not sure there is one!


It sounds like you could use DLookup, but since I never use and you
already have created qryGetDatesforS alesDate i'd do something like
what fallows instead..

Assuming that qryGetDatesforS alesDate is a parameter query that takes a
WeekEndingDate parameter [EndWeekDate], the following function will
return the MonthEndDate associated with it.

wkend = the WeekEndingDate you're working with.

<getMonthEndDat e type='aircode'>

Function getMonthEndDate (wkend As Date) As Date
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.Query Defs("qryGetDat esforSalesDate" )
qdf.Parameters( "EndWeekDat e") = wkend
Set rs = qdf.OpenRecords et

With rs
If Not .EOF Then
getMonthEndDate = .Fields("MonthE ndDate")
End If
.Close
End With

Set rs = Nothing
Set qdf = Nothing
End Function

</getMonthEndDate '>


Nov 13 '05 #6
I'm getting an error
"Item not found in this collection" on the following statement:

qdf.Parameters( "Forms!F_PrintS alesReports!Sal esDate") = wkEnd

I've tried
qdf.Parameters( Me.getWkMoDate) ; (getWkMoDate) (and every other idea and
combination I could think of)
- and I keep getting the same error

It's being called from the form above, and the user is entering the Week
End Date into a field called "getWkMoDat e". When I mouse over
"GetWkMoDat e" in the code it shows the date I put in the form.
Thanks again.
sara

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #7
rkc
sara wrote:
I'm getting an error
"Item not found in this collection" on the following statement:

qdf.Parameters( "Forms!F_PrintS alesReports!Sal esDate") = wkEnd

I've tried
qdf.Parameters( Me.getWkMoDate) ; (getWkMoDate) (and every other idea and
combination I could think of)
- and I keep getting the same error

It's being called from the form above, and the user is entering the Week
End Date into a field called "getWkMoDat e". When I mouse over
"GetWkMoDat e" in the code it shows the date I put in the form.
Thanks again.
sara


I am working blind here because you haven't shown the sql
to qryGetDatesforS alesDate. I am assuming that it is a parameter
query and that the parameter is named "EndWeekDat e". It probably
isn't so you have to substitute the actual parameter name where
you find "EndWeekDat e" in the function I posted.

Once you get that correct you call the function from your form
like so

Forms!F_PrintSa lesReports!Sale sDate = getMonthEndDate (Me.getWkMoDate )

That should fill Forms!F_PrintSa lesReports!Sale sDate with the
value returned from the function, which should be the MonthEndDate
associated with the date entered in Me.getWkMoDate. That also
assumes that there is a field returned by your query named
MonthEndDate which is used in the

getMonthEndDate = .Fields("MonthE ndDate")

line of the function.


Nov 13 '05 #8
I don't know why you are having trouble displaying dates. If your
fiscal month end doesn't conform to rules you will have to use the
dates in your table. The 53rd week would not have caused problems with
what I posted though. I'll let rkc help you get those dates.
James A. Fortune

Nov 13 '05 #9
I'm kind of lost here and a little frustrated. First, here is the SQL
of the query:

SELECT ALLDates.SalesD ate, ALLDates.DayofY ear, ALLDates.DOW,
ALLDates.Fiscal WeekNum, ALLDates.WeekSt artDate, ALLDates.WeekEn dDate,
ALLDates.Fiscal MonthNum, ALLDates.MonthS tartDate, ALLDates.MonthE ndDate,
ALLDates.Fiscal QtrNum, ALLDates.QtrSta rtDate, ALLDates.QtrEnd Date,
ALLDates.YearNu m, ALLDates.YearSt artDate, ALLDates.YearEn dDate,
ALLDates.LYSale sDate, ALLDates.LYWeek StartDate, ALLDates.LYWeek EndDate,
ALLDates.LYMont hStartDate, ALLDates.LYMont hEndDate,
ALLDates.LYQtrS tartDate, ALLDates.LYQtrE ndDate, ALLDates.LYYear Num,
ALLDates.LYStar tDate, ALLDates.LYEndD ate, ALLDates.Specia l
FROM ALLDates
WHERE
(((ALLDates.Sal esDate)=[Forms]![F_PrintSalesRep orts]![getWkMoDate]));

It simply takes a date that was entered on the form (the user is
entering WeekEndDate) and finds the associated dates in our Fiscal
calendar.

My problem is that the report I run needs the MonthEndDate as its
parameter, and it runs weekly. It 'builds' - the first week is just
week 1 data; the second week is weeks 1 and 2, etc., until the entire
month is filled in.

I keep getting the error "Item not found in this collection" on the
statement:
qdf.Parameters( "WeekEndDat e") = wkend
(You are right about the assumptions about the field names - but it's
WeekEndDate).

My code above is:

'Call the Function to get the Month Ending Date for the Spags weekly
Build report
' Call GetMoEndDate(Mo EndDate)

' MsgBox MoEndDate

Forms!F_PrintSa lesReports!Sale sDate = getMonthEndDate (Me.getWkMoDate )

MsgBox MoEndDate
stDocName = "rptSpagsSalesb yWeekTYLY"
On Error GoTo PrintSalesRepor ts_Err
strStepErrorMsg = "Tell IT there was a problem with Weekly
Spags store sales"
DoCmd.OpenRepor t stDocName, acViewPreview
And the function is:

Function getMonthEndDate (wkend As Date) As Date
'From Web
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.Query Defs("qryGetDat esforSalesDate" )
qdf.Parameters( "WeekEndDat e") = wkend

Set rs = qdf.OpenRecords et

With rs
If Not .EOF Then
getMonthEndDate = .Fields("MonthE ndDate")
End If
.Close
End With

Set rs = Nothing
Set qdf = Nothing
End Function

What I'm trying to learn/understand is how to code the function to
return a value - I seem to be all mixed up with parameters. I "get" it
in queries, and having a form "run" a query. Though I have probably
frustrated you and James, it is unintentional, I assure you. I have a
hugely better understanding of sub vs function now, but still don't feel
I can do it on my own and my books and Help - don't (help, that is).

Thanks for sticking with me. I did try DLookup and think I could get
that to work, if that makes more sense at this time.
Continuing thanks -
sara

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

63
3534
by: Papadopoulos Giannis | last post by:
Which do you think is best? 1. a) type* p; b) type *p; 2. a) return (var); b) return(var); c) return var;
144
6982
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
8
1812
by: s.subbarayan | last post by:
Dear all, In one of our projects in a document about C coding standard it is stated as "Always check a pointer is NULL before calling free. Always set a free'd pointer to NULL to try to protect it from being used again later and causing memory leaks." My doubt is,"Is this standard practice every where?"Also is it valid to free a pointer after the value is set to NULL?Because AFAIK,NULL means it points to nowhere!
12
6350
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square 0. but thats not i want to do. I want to recurse back until until it find a correct number. i will post the function which i need the help; ---coding----------------------------------------------------------
14
2419
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I think I need some "template". Sorry for my coding experience in c++
19
2017
by: tobiah | last post by:
def foo(thing): if thing: return thing + 1 else: return -1 def foo(thing): if thing:
7
4970
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C Programming Language Secure Coding Standard"
1
5054
by: R69D | last post by:
Write and test the following method that implements the power function: static double pow(double x, int n) This method returns the value of x raised to the power n. For example pow(2.0, -3) would return 2-3 = 0.125 For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.
19
3981
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4. Framework-Specific Guidelines Naming Conventions and Styles
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.