Results 1 to 10 of 10
  1. #1
    qcjustin is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18

    Angry Help with DCOUNT Function in Module

    I am trying to adjust my DCOUNT function to also use Country field as a criteria to determine if a date is a Holiday.
    I have a table (Holidays) with 2 fields on it. HolDate and Country.

    In my query run, the field for Country is called CountryOn.

    This runs, but it does not identify a date as a holiday even though its in the table.
    The matching problem is in Country.
    I would like it to count the number of instances where the date AND country match, since different countries have different holidays.
    lCountry is set during the input query for my OTHER public function.
    Can i define a variable in a function, and have the other public function reference it?
    CountryOn must be a dynamic variable, as there are queries with many different names these functions run from.

    Right now I have a query for each country.
    The main function references IsHoliday function.

    Main Function Call
    Public Function NetWorkMinutes(rdteStart As Date, rdteEnd As Date, lCountry As String) As Long

    Holiday Function
    Public Function IsHoliday(rdteDate As Date) As Boolean
    Dim lCountry As String
    On Error GoTo IsHoliday_Error

    Select Case DCount("*", "Holidays", "HolDate = " & DateSQL(rdteDate) _
    & "Country" = lCountry)
    Case Is > 0
    IsHoliday = True
    Case Else
    IsHoliday = False
    End Select




    Exit_Procedure:


    On Error GoTo 0
    Exit Function


    IsHoliday_Error:


    If Err.Number = 3078 Then
    IsHoliday = False
    Resume Exit_Procedure
    Else
    Err.Raise Err.Number, Err.Source, Err.Description
    End If

  2. #2
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    As long as the lCountry variable is declared as public then yes, you can access it from any Function in the database.

    Perhaps a better option though, would be to pass the lCountry into your IsHoliday() function as an argument. Assuming it's set in the parent function, that shouldn't be too difficult.

  3. #3
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18
    Since I am not setting the arguments for IsHoliday(the NetWorkMinutes function calls this function/variables), is this still the best way to do it?

    I have modified my IsHoliday line to be : Public Function IsHoliday(rdteDate As Date, Optional lCountry As String) As Boolean

    If I leave out optional, it says "Compile Error(Argument not optional) for another line:

    If IsHoliday(rdteStart) Then

    I have added it as a normal required variable, and also added it to each statement. If IsHoliday(rdteStart, lCountry)

    It still does not count it as a holiday.

  4. #4
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18

    Attachment

    Here is an attachment of a test database for this.
    I have printed the values for Country and Date to a message box, and they look to be correct.

    The Response time for the single entry on there should change from 9 hours down to 1 hour, if it works right.

    Thanks much
    Attached Files Attached Files
    Last edited by qcjustin; 04-08-2015 at 09:44 AM.

  5. #5
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18

    Anyone?

    This has to be a pretty simple thing missing.

  6. #6
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18
    My latest change is adjusting my DCOUNT line to this.

    Since lCountry is a text string, i used:

    Select Case DCount("*", "Holidays", "HolDate = " & DateSQL(rdteDate) & " And [Country] Like '& lCountry & '")

    This looks right to me, but DOES NOT WORK.

    Any help would be appreciated.
    Last edited by qcjustin; 04-07-2015 at 10:11 PM.

  7. #7
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18
    Bump... need an answer

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The syntax would be

    DCount("*", "Holidays", "HolDate = " & DateSQL(rdteDate) & " And [Country] Like '" & lCountry & "'")

    unless you wanted a wildcard in there. That assumes the date function returns an appropriate value complete with #.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    qcjustin is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Dec 2012
    Location
    Washington
    Posts
    18
    Worked like a charm, just as simple as some quotes misplaced. Thanks a bunch.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. dcount function - new to Access VBA
    By jillp in forum Programming
    Replies: 7
    Last Post: 09-20-2012, 06:35 AM
  2. Module & dcount problem
    By gg80 in forum Modules
    Replies: 5
    Last Post: 01-20-2012, 07:12 PM
  3. DCOUNT function with IN statement
    By dwilson in forum Access
    Replies: 2
    Last Post: 08-12-2011, 09:54 AM
  4. Using a date range with Dcount function
    By mleberso in forum Reports
    Replies: 4
    Last Post: 06-17-2011, 08:56 AM
  5. DCount function problem
    By 10 Gauge in forum Forms
    Replies: 5
    Last Post: 02-28-2011, 02:08 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