Results 1 to 8 of 8
  1. #1
    Cafe2UEMP is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2017
    Posts
    8

    Problem with Variable in DAvg

    Hi Folks



    I am sorry to post this as it must be a really simple issue but I just cannot get it!

    I have an expression in a query which returns the current year as CY: Format([Date1],"yyyy",2,2)

    I then have a Public Function as:

    Public Function AvePerDayTY()


    Dim ThisYear As Integer


    ThisYear = Year(Date)


    AvePerDayTY = Nz(DAvg("[TotalSales]", "QryYTDSales", "CY=ThisYear"))
    End Function

    This produces an error. If I change the criteria to actual year, eg. 2017, the function works.

    What am I doing wrong here?

    Many thanks

    David

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you had Thisyear inside the quotes...it must be outside to resolve...

    Nz(DAvg("[TotalSales]", "QryYTDSales", "CY=" & ThisYear))

  3. #3
    Cafe2UEMP is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2017
    Posts
    8
    Thank you so much. That works perfectly!

    I am now trying to add a second criteria but keep getting lost on the syntax.

    I have added ThisDay = Weekday(Date, 2) + 1 as a variable (integer) and now wish to add this as criteria two so that the function finds this day in this year.

    Are you able to assist?

    Much appreciated.

    David

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Getting the current day of week doesn't help get year to date average. Don't you need the number of days elapsed? Use DateDiff function or:

    ThisDay = Int(Format(Date(), "y"))

    What field has the day number? If you have not calculated a day number in query, do it in the DAvg()

    Nz(DAvg("TotalSales", "QryYTDSales", "CY=" & ThisYear & " AND Int(Format(DateField, 'y'))<=" & ThisDay))

    or

    Nz(DAvg("TotalSales", "QryYTDSales", "DateField BETWEEN #1/1/" & Year(Date()) & "# AND Date()"))
    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
    Cafe2UEMP is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2017
    Posts
    8
    Quote Originally Posted by Cafe2UEMP View Post
    Thank you so much. That works perfectly!

    I am now trying to add a second criteria but keep getting lost on the syntax.

    I have added ThisDay = Weekday(Date, 2) + 1 as a variable (integer) and now wish to add this as criteria two so that the function finds this day in this year.

    Are you able to assist?

    Much appreciated.

    David
    Thanks again for the assistance.

    The weekday is calculated in the query so no problems there.

    I wasn't clear enough in my post, sorry.

    What I am trying to achieve is a comparison between sales for a day (say Friday) compared with average sales for all Fridays in a given year so I need the function to calculate averages per day.

    Many thanks
    David

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Okay, use your field name and day of week calc and change <= to just =.
    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.

  7. #7
    Cafe2UEMP is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2017
    Posts
    8
    Quote Originally Posted by ranman256 View Post
    you had Thisyear inside the quotes...it must be outside to resolve...

    Nz(DAvg("[TotalSales]", "QryYTDSales", "CY=" & ThisYear))
    Sorry but I am not too sure what you mean.

    This is what I have to date which produces the average per day for a year.

    Public Function AvePerDayTY()


    Dim ThisYear As Integer
    Dim ThisDay As Integer






    ThisYear = Year(Date)
    ThisDay = Weekday(Date, 2) + 1


    AvePerDayTY = Nz(DAvg("[TotalSales]", "QryYTDSales", "CY=" & ThisYear))


    End Function

    All I am trying to do is add an "And" statement to include "ThisDay" so I am looking at an average for that day in that year.

    Thanks again

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Still not clear what you want.

    I already gave you 2 examples with " AND " operator in post 4. The function will return the average of all records meeting the criteria.

    Do you want the average of records for a single Friday? Then use a full date value. If you want to average records for Friday of the current week:

    Nz(DAvg("[TotalSales]", "QryYTDSales", "DateField=Date()+(6-Weekday(Date()))"))

    The constant 6 restricts this to calculate only a Friday date. If you want to average records based on value in DateField, consider:

    SELECT *, Nz(DAvg("TotalSales", "QryYTDSales", "DateField=" & DateField)) AS DayAverage FROM tableORquery;


    You are calling the UDF from a query? Perhaps you should provide the query SQL statement. Also, a sample of raw data as well as desired output can be helpful. I suspect VBA is not really necessary.
    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.

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

Similar Threads

  1. DAVG in query
    By amai in forum Queries
    Replies: 2
    Last Post: 04-01-2016, 10:08 AM
  2. Using DAvg function
    By GraeagleBill in forum Programming
    Replies: 2
    Last Post: 04-03-2015, 11:03 AM
  3. DAvg Not Working
    By BLFOSTER in forum Forms
    Replies: 3
    Last Post: 04-21-2014, 11:14 AM
  4. Problem with DAvg function
    By averagejoe in forum Access
    Replies: 1
    Last Post: 10-05-2012, 05:43 AM
  5. Replies: 4
    Last Post: 08-05-2010, 01:26 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