Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

MS Access 2003 - Undefined Function

  • 23-09-2010 8:55am
    #1
    Registered Users, Registered Users 2 Posts: 896 ✭✭✭


    Hi all, I've written a small bit of VB code to calculate the number of working days between two dates. I get the message "Undefined Function ' WorkDays2' in expression".

    I've tried a few tricks, checked for missing references, added and then removed a random reference. Any thoughts?

    Public Function WorkDays2(dteStart As Date, dteEnd As Date) As Long
    
    WorkDays2 = 0
    
    If dteStart <> 31 / 12 / 9999 Then
        Do While dteStart <> dteEnd
            dteStart = DateAdd("d", 1, dteStart)
            If Weekday(dteStart, vbMonday) <= 5 Then
                WorkDays2 = WorkDays2 + 1
            End If
        Loop
    End If
                
    End Function
    


Comments

  • Closed Accounts Posts: 1,759 ✭✭✭Dr.Silly


    have you tried defining what workdays2 is

    something like

    dim workdays2 as int
    set workdays 2 = 0


  • Registered Users, Registered Users 2 Posts: 896 ✭✭✭Fuzzytrooper


    WorkDays2 is the function name and hence the return type. I just double checked as you suggested but this threw up a compile error (Duplicate declaration)


  • Closed Accounts Posts: 1,759 ✭✭✭Dr.Silly


    this jsut worked for me, just did a button with a message box calling the function.
    So you're code works fine, I imagine it's the date format which you're passing in.

    do
    msgbox date
    and see what format your date comes out as, should work fine.


    Private Sub Command1_Click()
    MsgBox WorkDays2("23/09/2010", "24/09/2010")


    End Sub
    Public Function WorkDays2(dteStart As Date, dteEnd As Date) As Long

    WorkDays2 = 0

    If dteStart <> 31 / 12 / 9999 Then
    Do While dteStart <> dteEnd
    dteStart = DateAdd("d", 1, dteStart)
    If Weekday(dteStart, vbMonday) <= 5 Then
    WorkDays2 = WorkDays2 + 1
    End If
    Loop
    End If

    End Function


  • Registered Users, Registered Users 2 Posts: 3,429 ✭✭✭dnme


    I'd refactor it like this, use a dedicated variable in your calculation, especially as it's used in a loop

    Public Function WorkDays2(dteStart As Date, dteEnd As Date) As Long

    Dim ret as intreger
    ret = 0

    If dteStart <> 31 / 12 / 9999 Then
    Do While dteStart <> dteEnd
    dteStart = DateAdd("d", 1, dteStart)
    If Weekday(dteStart, vbMonday) <= 5 Then
    ret = ret + 1
    End If
    Loop
    End If

    WorkDays2 = ret

    End Function


Advertisement