Results 1 to 7 of 7
  1. #1
    Poyan is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    May 2011
    Location
    Toronto, Canada
    Posts
    3

    Where to define VBA Functions?

    Where do I define a user-defined VBA functions so that I can call them in the query?



    Here is the problem. I am defining a VBA functions called DateDiffW which calculates the number of weekdays (i.e. excludes sat and sun) between 2 dates.

    In access 2007, under the "create" tab I clicked "macro" -> "module". Then I defined the function there, starting with:

    Function DateDiffW(BegDate, EndDate)

    I then saved the module.

    In the query, when I call the DateDiffW function I get the error:

    "Undefined Function 'DateDiffW' in expression"

    What am I doing wrong?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Looks like you did everything correctly so don't know why the query can't find the function. This shouldn't make difference but try anyway.
    Public Function DateDiffW(BegDate As Date, EndDate As Date) As Integer

    Test the function by calling it in the VBA Immediate window.

    Post the entire procedure code for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Poyan is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    May 2011
    Location
    Toronto, Canada
    Posts
    3
    Never mind. It was a security setting blocking my macros. Thanks for your help.

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by June7 View Post
    Looks like you did everything correctly so don't know why the query can't find the function. This shouldn't make difference but try anyway.
    Public Function DateDiffW(BegDate As Date, EndDate As Date) As Integer

    Test the function by calling it in the VBA Immediate window.

    Post the entire procedure code for analysis.
    The Public part won't matter because it is the default. But I think you nailed it in that the function that the OP posted doesn't have a return type defined.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    It is certainly good practice to declare datatypes for variables but turns out that wasn't the issue. I should have considered security issue. Must be OP's first code in the project.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Interesting, I always had thought that a function that didn't have a return type specified would not return anything. But just like an untyped variable, it returns a variant type from what I can tell, because if a value for it is set, it returns that and if you just have a blank shell:

    Function GetVal()
    End Function

    it returns an empty string.

    So, another thing learned

  7. #7
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    Quote Originally Posted by boblarson View Post
    Interesting....
    Function GetVal()
    End Function

    it returns an empty string.

    So, another thing learned
    I thought it returns nothing....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 06-03-2011, 09:03 AM
  2. sum functions
    By trippers in forum Queries
    Replies: 2
    Last Post: 08-04-2010, 07:09 PM
  3. Replies: 6
    Last Post: 07-22-2010, 05:53 PM
  4. Define a date range for a report - Help
    By Optimus_1 in forum Access
    Replies: 4
    Last Post: 06-02-2010, 04:50 AM
  5. Replies: 3
    Last Post: 02-16-2010, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums