Business Day Code HELP PLEASE (1 Viewer)

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Hello,

I am trying to use the code I found here in the forum to calculate an existing date and adding a number of days to get the new business date...I have a query in which I am using the field "DateARAgingRcvd" (which is a date field) and adding 10 to get the new date which would be "DateFirstMailingDue".

I copied the code into a new module, saved and named it and tried to apply it to my query like below...

FirstMailingDue: GetWorkDateDiff ([DateARAgingRcvd],10)

I get this error message: Undefined Function "GetWorkDateDiff" in expression

So I changed Private to Public in the code

Then I get this message:

The expression you entered has a function with the wrong number of arguments.

Below is the code I am using...I am new to code so please forgive me.


Option Compare Database
Option Explicit

Public Sub btnSubmit_Click()
lblResultEndDate.Caption = GetWorkDateDiff(dtpDate.value, txtWorkDays.value, CBool(chkCurrentDay.value))
End Sub

Public Function GetWorkDateDiff(start_date, days, count_first_day As Boolean) As Date
If isValidValue(start_date, "Date") Then
If isValidValue(days, "Number") Then
Dim work_days As Integer: work_days = CInt(days)
Dim due_date As Date: due_date = DateAdd("d", IIf(count_first_day, 0, -1), start_date)

While work_days > 0
due_date = DateAdd("d", 1, due_date)
If Not (Weekday(due_date) = 1 Or Weekday(due_date) = 7) Then: _
work_days = work_days - 1
Wend

GetWorkDateDiff = due_date
Else
MsgBox "Invalid Value, must be non-null and a whole number"
End If
Else
MsgBox "Invalid Value, must be non-null and date value."
End If
End Function

Public Function isValidValue(value, type_name) As Boolean

isValidValue = (Len(value & vbNullString) > 0 And _
IIf( _
TypeName(value) = "string" And type_name = "number", _
IsNumeric(value), _
type_name = TypeName(value) _
) _
)
End Function


I thank you for any help.

Lilly
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
you have done enough posts by now to know that

a) you enclose code in code tags
b) you indent code to make it readable
c) explain where you are getting the error - what line of code? what values are you passing, etc
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
I did not know the rules of posting code to the forum so my apologies.

Do you know of a more generic code that would accomplish what I was trying to do-which is taking an existing date field and adding a number to get the next business day date?

When I go into VBA and run it, it highlights the first line in yellow (see below)-I tried to type it the way I see it...

Public Sub btnSubmit_Click()
lblResultEndDate.Caption = GetWorkDateDiff(dtpDate.value, txtWorkDays.value, CBool(chkCurrentDay.value))
End Sub

I don't have a button named Submit in my Database so I deleted that out and ran it again and it just brings up Macros on the very next line and that is it--will not go any further and I don't understand what it is doing.

Code: After deleting above line...

Public Function GetWorkDateDiff(start_date, days, count_first_day As Boolean) As Date
If isValidValue(start_date, "Date") Then
If isValidValue(days, "Number") Then
Dim work_days As Integer: work_days = CInt(days)
Dim due_date As Date: due_date = DateAdd("d", IIf(count_first_day, 0, -1), start_date)

While work_days > 0
due_date = DateAdd("d", 1, due_date)
If Not (Weekday(due_date) = 1 Or Weekday(due_date) = 7) Then: _
work_days = work_days - 1
Wend

GetWorkDateDiff = due_date
Else
MsgBox "Invalid Value, must be non-null and a whole number"
End If
Else
MsgBox "Invalid Value, must be non-null and date value."
End If
End Function


Thanks for trying to help me.

Lilly
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Sorry, when I am typing the code in here, I am using spaces and indents but when I submit the reply it does not stay...so not sure what I am doing wrong.

Lilly
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
enclose the code with code tags - you'll find them on the advanced editor


the code tags are the hash button

 

Attachments

  • Capture.JPG
    Capture.JPG
    29.2 KB · Views: 199
  • Capture1.JPG
    Capture1.JPG
    24.1 KB · Views: 201

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Code:
Option Compare DatabaseOption ExplicitPrivate Sub btnSubmit_Click()    lblResultEndDate.Caption = GetWorkDateDiff(dtpDate.value, txtWorkDays.value, CBool(chkCurrentDay.value))End SubPrivate Function GetWorkDateDiff(start_date, days, count_first_day As Boolean) As Date    If isValidValue(start_date, "Date") Then        If isValidValue(days, "Number") Then            Dim work_days As Integer: work_days = CInt(days)            Dim due_date As Date: due_date = DateAdd("d", IIf(count_first_day, 0, -1), start_date)                        While work_days > 0                due_date = DateAdd("d", 1, due_date)                If Not (Weekday(due_date) = 1 Or Weekday(due_date) = 7) Then: _                    work_days = work_days - 1            Wend                        GetWorkDateDiff = due_date        Else            MsgBox "Invalid Value, must be non-null and a whole number"        End If    Else        MsgBox "Invalid Value, must be non-null and date value."    End IfEnd FunctionPrivate Function isValidValue(value, type_name) As Boolean        isValidValue = (Len(value & vbNullString) > 0 And _                    IIf( _                        TypeName(value) = "string" And type_name = "number", _                            IsNumeric(value), _                        type_name = TypeName(value) _                        ) _                    )End FunctionAttached Files[IMG]http://images.access-programmers.co.uk/forums/images/attach/mdb.gif[/IMG][URL="http://www.access-programmers.co.uk/forums/attachment.php?attachmentid=56008&d=1412344624"][COLOR=#800080]WorkDaysTillDueFunc.mdb[/COLOR][/URL] (376.0 KB, 30 views)
Did I do this right? Thank you.
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Code:
[COLOR=black][FONT=Verdana]Option Compare Database[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Option Explicit[/FONT][/COLOR]
[COLOR=black][FONT=Verdana] [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Public Sub btnSubmit_Click()[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    lblResultEndDate.Caption = GetWorkDateDiff(dtpDate.value, txtWorkDays.value, CBool(chkCurrentDay.value))[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]End Sub[/FONT][/COLOR]
[COLOR=black][FONT=Verdana] [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Public Function GetWorkDateDiff(start_date, days, count_first_day As Boolean) As Date[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    If isValidValue(start_date, "Date") Then[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]        If isValidValue(days, "Number") Then[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            Dim work_days As Integer: work_days = CInt(days)[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            Dim due_date As Date: due_date = DateAdd("d", IIf(count_first_day, 0, -1), start_date)[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            While work_days > 0[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                due_date = DateAdd("d", 1, due_date)[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                If Not (Weekday(due_date) = 1 Or Weekday(due_date) = 7) Then: _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                    work_days = work_days - 1[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            Wend[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            GetWorkDateDiff = due_date[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]        Else[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            MsgBox "Invalid Value, must be non-null and a whole number"[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]        End If[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    Else[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]        MsgBox "Invalid Value, must be non-null and date value."[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    End If[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]End Function[/FONT][/COLOR]
[COLOR=black][FONT=Verdana] [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Public Function isValidValue(value, type_name) As Boolean[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    isValidValue = (Len(value & vbNullString) > 0 And _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                    IIf( _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                        TypeName(value) = "string" And type_name = "number", _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                            IsNumeric(value), _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                        type_name = TypeName(value) _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                        ) _[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                    )[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]End Function[/FONT][/COLOR]
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
almost there, but for some reason your code is losing all it's carriage returns/line feeds. You should just be copying the code from your vba window into the advanced editor window, then highlight what you have copied and click on the code button

Code:
Public Sub btnSubmit_Click()
    lblResultEndDate.Caption = GetWorkDateDiff(dtpDate.value, txtWorkDays.value, CBool(chkCurrentDay.value))
End Sub

Now seems OK
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
OK, I can't see anything wrong with the code so it comes down you how you are using it here
Code:
FirstMailingDue: GetWorkDateDiff ([DateARAgingRcvd],10)
the GetWorkDateDiff function has 3 paramaters and you are only supplying two

try
Code:
FirstMailingDue: GetWorkDateDiff ([DateARAgingRcvd[COLOR=black]],10[/COLOR][COLOR=red], False[/COLOR])
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Thank you...I did as you said and changed the query... it ran but then after the datasheet opened, I scrolled over to look at the dates and it gave me the message...

Query: 1stMailingDue: GetWorkDateDiff([DateARAgingRcvd],14,False)


Message:

Invalid Value, must be non-null and a whole number...

and it froze...

Again, sorry for not posting correctly...I will do better next time.

Thank you for helping me.

Lilly
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
if you look at the function, that is an error message generated by the function, so it is telling you that the date field is not a date - so I would check your data - you may be missing a date or two
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Good morning,

Yes, I did realize that and did check the data in my table that is pulling that field and it is Date/Time...

So, I made a test table put in one field...Formatted to Date (short date which is 4/5/15...pulled that into a new query and tried to apply the function with that field and same thing so I have not idea what is going on and don't understand code enough to figure it out...but if I push the data through I get this...

DateTest TestDate
2/5/2015
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Sorry it ended up sending before I finished...but the result came out to 12:00:00 AM ... no date and then it freezes up.

If you don't have time to help, I totally understand...I found the DateAdd function that I am trying and it seems to be working but I am still testing the data...so I can probably use this function instead...

Thank you.

Lilly
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
This is what I am trying and it seems to be working...

FirstMailingDue: DateAdd("w",14,[DateARAgingRcvd])

Do you see any problems with this that I may not?

Again, thank you for the help.:eek:

Lilly
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
Yes, I did realize that and did check the data in my table that is pulling that field and it is Date/Time...
my point was that you may have been missing some dates - i.e. value is null
So, I made a test table put in one field...Formatted to Date (short date which is 4/5/15
format is just a way of formatting the date, the underlying value remains the same and is what is used in calculations etc. Date is actually a decimal number, the value to the left of the decimal point represents the date and to the right, the time expressed as a percentage of the number of seconds in the day (24hrs x 60mins x 60 secs)

but the result came out to 12:00:00 AM
this implies that your date value is actually date/time - perhaps you have populated the field with now() rather than date()?

your error message says
...and a whole number

and a date with a time element is not a whole number - this may be why it is failing


FirstMailingDue: DateAdd("w",14,[DateARAgingRcvd])

Do you see any problems with this that I may not?
No problem, but 'w' used in this context is the same as 'd' so this code will add 14 days (you could also use 'ww' and 2 for two weeks)
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Thank you.

I just don't understand I guess...

I am using a test table with one record in it which that field is set to Date/Time...this appears to be the only choice I have for date, then I pulled that table into a new query and tried to use the function in the same way we did yesterday with the same result so I just don't get it...there are no null values in that table...just one record...and I know that you said "perhaps you have populated the field with now() rather than date() " so not sure where I would have done that...can you explain?

Again, sorry for not getting it...I wish I could get this to work ...

Thanks for the better understanding of the "d" and "w"...appreciate it.

Again, thanks for any help you have been very kind.

Lilly
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:01
Joined
Feb 19, 2013
Messages
16,637
I don't have time to investigate the function (although I can't see anything wrong with it in principle) so you could go back to where you got it from and ask there

Date() and now() are standard functions to return today (26/3/2015-->42089) and the current time (26/3/2015 02:00PM-->42089.583)

you might use them for example in the context of a default value for a date field in a table or comparison in a query e.g. projectedDate>Date()
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:01
Joined
Jan 23, 2006
Messages
15,390
lilly240,

Can you tell us specifically what you are trying to do -- plain English?
Once readers understand the issue, I'm sure more focused responses will follow.
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Hello,

Certainly, I found code on this forum which I thought I could use to accomplish the below but was having issues so reached out for help.

I am trying to take a date field from a table which is callded "DateARAgingRcvd" and wanted to add 10 business days to this to get a new field called "FirstMailingDueDate" and was trying to do this using the function I found here in a query like below.

FirstMailingDueDate: GetWorkDateDiff([DateARAgingRcvd],10,False)

but it won't run...and I don't know how to fix it.

I hope this makes sense...and thank you for any help.

Lilly
 

Lilly420

Registered User.
Local time
Today, 18:01
Joined
Oct 4, 2013
Messages
126
Hello,

I am not going to use the code in this post so no need to try to help on this...I came up with a way to accomplish what I need without using it...I was going to use the DateAdd function but my fear with that was if one the users adds a weekend date when logging in the ARs (and they have) this will throw off my 10 business day calculation to meet the First Mailing Due Date...but I was able to eliminate that by using the below...and it works great with all the data that I tested thus far...so thank you all for trying to help and I hope the below will help someone else should they need it.

The goal is to have a new field created called "FirstMailingDueDate" 10 business days from the date of the field called "DateARAgingRcvd" ...

1stMailingDue: IIf(Weekday([DateARAgingRcvd])=1,[DateARAgingRcvd]+15,IIf(Weekday([DateARAgingRcvd])=7,[DateARAgingRcvd]+16,[DateARAgingRcvd]+14))

Lilly
 

Users who are viewing this thread

Top Bottom