Computer Learning Zone CLZ Access Excel Word Windows

Intelligence is the ability to adapt to change.

-Stephen Hawking
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Visitor Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
MyNetWorkDays function
Ben Davis 

2 years ago
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 Davis OP  @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?

Again thanks for any help,
Ben
Alex Hedley  @Reply  
           
2 years ago
From Work Days
Richard Rost  @Reply  
          
2 years ago
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 Davis OP  @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.

Kind Regards,
Ben
Richard Rost  @Reply  
          
2 years ago
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 Davis OP  @Reply  

2 years ago
You know what, I did exactly that not knowing this could cause an issue.

Thanks for all your help!

Ben
Richard Rost  @Reply  
          
2 years ago
Yep. That'll do it.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Visitor Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 7:39:28 AM. PLT: 1s