Hi, I was looking for a way to calculate network days and found your video. I've attempted to create the function in VBA just as you show/say in the video (and included addendum) however when i try to use it within a query it gives me the error "Undefined function 'MyNetWorkDays" in expression". For Clarification i want to calculate the MyNetWorkDays between a DueDate Field and the current date. The VBA code im using is: Details"Option Compare Database Option Explicit
Public Function MyNetWorkDays(StartDate As Date, EndDate As Date) As Long
Dim D As Date MyNetWorkDays = 0 D = StartDate While D <= EndDate If Weekday(D) > 1 And Weekday(D) < 7 Then MyNetWorkDays = MyNetWorkDays + 1 D = D + 1 Wend End Function"
I dont believe there is an issue with this, but if there is id love to know.
The expression i am trying to use is to calculate the difference is: Over: MyNetWorkDays([DueDate],Date())
Any help is appreciated, Thanks, Ben
Kevin Robertson
@Reply 2 years ago
First thing I would do is check the spelling. Have you spelled the Function name correctly in your Query?
Did you put the code in a Module? Did you accidently click on Class Module instead of Module?
Note: I tested the code and it works perfectly.
Ben DavisOP
@Reply
2 years ago
Hi Kevin, I'm perplexed as to why it isn't working for me then. The VBA code is definitely in a Module not Class Module or Procedure. I have checked spellings and all seems ok, the spelling i have used for both the VBA code and the query expression are exactly as it is in my initial question, i C+P'd directly from the module/query to ensure i was giving an accurate account of my problem. I've even gone as far as to compile the VBA code, and compact and repair the database to no avail.
I decided to start again and watch the video from scratch. I noticed that when i open the module i dont have the line that says "Option Explicit". Because i was missing it i initially just added it in, i've tried removing it and its still coming up with the "Undefined Function..." error message, for reference i tried with and without also removing the line "Dim D As Date".
Given this new information is there anything you can think of?
Sounds like you missed something, but without seeing your whole database, it's tough to tell. Try copying the function to a module in a brand new database. Run down the Troubleshooter.
Ben DavisOP
@Reply
2 years ago
Hi All, I went back and looked at another VBA code I had for another function I created that works. That code didn't have the word 'Public' at the start of line equivalent to this "Public Function MyNetWorkDays(StartDate As Date, EndDate As Date) As Long". I went ahead and made that change, still nothing. Out of curiosity I went ahead and also change the name of the function, and due to my modules not opening with Option Explicit I removed that and the line "Dim D As Date" as mentioned in the video.
The code I was then left with was:
"Option Compare Database
Function X(StartDate As Date, EndDate As Date) As Long
X = 0
D = StartDate
While D <= EndDate
If Weekday(D) > 1 And Weekday(D) < 7 Then X = X + 1
D = D + 1
Wend
End Function"
This is now working as expected. I did change "X" back to "MyNetWorkDays" and it errored again. Don't know why, but my VBA doesn't appear to like the use of that name. Fortunately it doesn't need to be named that.
I consider this matter solved but not understood.
Richard, If you would like to investigate this to try to understand the issue feel free to reach out via my email address. I would be more than happy with this.
That's really weird. The only thing at this point I can think of is, did you happen to name the module itself MyNetWorkDays? I've done that before too. I've named the module the same thing as one of the functions in it, and that caused the problem.
Ben DavisOP
@Reply
2 years ago
You know what, I did exactly that not knowing this could cause an issue.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Visitor Forum.