Results 1 to 5 of 5
  1. #1
    RonanM is offline Novice
    Windows XP Access 2000
    Join Date
    Jun 2011
    Posts
    5

    Query using a User defined function for dates

    Hi

    I have "attempted" to create a user defined function, to use to workout the actual days the fall within a Period. This is to save me needing to use a complex nested if statment in the Sql

    The function compiles (big deal), but it is not what I want.

    Code below:

    Code:
    Option Compare Database
    Option Explicit
    Public Function WorkingDaysInPeriod(StartDate As Date, EndDate As Date) As Integer
    'On Error GoTo Err_WorkingDaysInPeriod
    Dim intCount2 As Integer
    Dim Ans1 As Date
    Dim Ans2 As Date
    Ans1 = InputBox("Enter Period Start Date")
    Ans2 = InputBox("Enter Period End Date")
    Ans1 = DateValue(Ans1)
    Ans2 = DateValue(Ans2)
    Select Case WorkingDaysInPeriod(StartDate, EndDate)
    Case StartDate >= Ans1 _
    And StartDate <= Ans2 _
    And EndDate <= Ans2
    intCount2 = WorkingDays(StartDate, EndDate) + 1
    Case StartDate >= Ans1 _
    And StartDate <= Ans2 _
    And EndDate > Ans2
    intCount2 = WorkingDays(StartDate, Ans2) + 1
    Case StartDate >= Ans1 _
    And EndDate >= Ans1 _
    And EndDate <= Ans2
    intCount2 = WorkingDays(Ans1, EndDate) + 1
    Case StartDate < Ans1 _
    And EndDate > Ans2
    intCount2 = WorkingDays(Ans1, Ans2) + 1
    Case StartDate < Ans1 _
    And EndDate > Ans1 _
    And EndDate < Ans2
    intCount2 = WorkingDays(Ans1, EndDate) + 1
    Exit_WorkingDayInPeriods:
    Exit Function
    End Select
    'Err_WorkingDaysInPeriod:
    'Select Case Err
    '
    'Case Else
    'MsgBox Err.Description
    'Resume Exit_WorkingDaysInPeriod
    '
    'End Select
    End Function



    And this was the nested IIF staement in the sQL:

    Code:
    IIF(BD.[Class Start Date]>=[Please Enter Period Start Date] AND BD.[Class Start Date]<=[Please Enter Period End Date] AND BD.[Class End Date]>[Please Enter Period End Date],WORKINGDAYS(BD.[Class Start Date],[Please Enter Period End Date])+1,IIF(BD.[Class Start Date]>=[Please Enter Period Start Date] AND BD.[Class End Date]>=[Please Enter Period Start Date] AND BD.[Class End Date]<=[Please Enter Period End Date],WORKINGDAYS([Please Enter Period Start Date],BD.[Class End Date])+1,IIF(BD.[Class Start Date]<[Please Enter Period Start Date] AND BD.[Class End Date]>[Please Enter Period End Date],WORKINGDAYS([Please Enter Period Start Date],[Please Enter Period End Date])+1,IIF(BD.[Class Start Date]<[Please Enter Period Start Date] AND BD.[Class End Date]>[Please Enter Period Start Date] AND BD.[Class End Date]<[Please Enter Period End Date],WORKINGDAYS([Please Enter Period Start Date],BD.[Class End Date])+1,0)))))) AS Calculation

    If that makes sense to anybody, any assistance would be much appreciated...

    Thanks

    Ronan

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    You are calling the function within itself on the Select Case line. This is recursive, that what you want? What is the function WorkingDays? Call the function in the VBA immediate window and step debug, follow the code as it executes.
    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
    RonanM is offline Novice
    Windows XP Access 2000
    Join Date
    Jun 2011
    Posts
    5
    Nope, recursive isn't what I wanted, so I'll move that, and see what happens. I wondered why it kept prompting for the input box....

    Really, what I was attempting was to create the function so that it would look at the start and end dates in the a table, then use the parameters Enter start date and End date, to work out the days that fall within the period...

    The WorkingDays Function just counts the days Monday to Friday. And that works fine.

    Cheers

    Ronan

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    It's late and I can't see straight but Select Case might not be right here, maybe needs to be IF THEN ELSEIF.
    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.

  5. #5
    RonanM is offline Novice
    Windows XP Access 2000
    Join Date
    Jun 2011
    Posts
    5
    No worries... 11am here.... I'll have a play anyway. Cheers...

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

Similar Threads

  1. User Defined Sorting in Form
    By sparlaman in forum Forms
    Replies: 6
    Last Post: 04-26-2011, 12:02 PM
  2. Where to start: user defined reports
    By noweyout in forum Reports
    Replies: 2
    Last Post: 04-22-2011, 01:23 PM
  3. MakeTable Query with Variable user defined Name
    By Dinzdale40 in forum Programming
    Replies: 1
    Last Post: 03-09-2011, 11:26 AM
  4. Replies: 3
    Last Post: 08-24-2010, 09:26 AM
  5. Error: "User-defined type not defined"
    By mastromb in forum Programming
    Replies: 10
    Last Post: 01-08-2010, 02:57 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