Results 1 to 3 of 3
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,097

    Using DAvg function

    I might not be able to do this because of the "DATE" data-type of DOE, but I thought I'd post the question anyway. As one can see, I want to get the average value of the CLng of the "DOE" fields in the table "Ledgers". "AvgCriteria" seems to be formatting okay, as observed in Debug. The function is referenced as the RecordSource of an unbound text box. e.g., =PurchaseDate([InvstID])

    Is the expression "CLng([DOE])" valid as coded?


    BTW, TType is an autonumber field, so it's data type long.

    Code:
    Private Function PurchaseDate(ID As Long) As Date
    Dim AvgCriteria As String
    Dim AvgValue
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    ' For the given investment (ID), we need to calculate the average purchase date based on all "buy's"
    ' prior to the current year being processed, see "ForYear".  The process is simply a case of selecting all
    ' the "buy" DOE's for the current investment and calculating the average.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    AvgCriteria = "[InvstID] = " & ID & " AND [TType] = 1"
    AvgValue = DAvg("CLng([DOE])", "Ledgers", AvgCriteria)
    PurchaseDate = CDate(AvgValue)
    
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Why is this a function, and not a query?
    (Yes, cLng is right, but why you have An avg date)

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,097
    I tried a query but ran into data-type problems with trying to use the average function there as well. Anyway, I needed to get the section of the app up so I defaulted to what I know how to easily do. (Not sure what's behind your question regarding the averaging of dates.)

    New code:

    Code:
    Private Function PurchaseDate(ID As Long)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    ' For the given investment (ID), we need to calculate the average purchase date based on all "buy's"
    ' prior to the current year being processed, see "ForYear".  The process is simply a case of selecting all
    ' the "buy" DOE's for the current investment and calculating the average.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim rs As DAO.Recordset
    Dim strSql As String
    Dim Accumulator As Long
    Dim count As Integer
    
    count = 0
    Accumulator = 0
    
    strSql = "SELECT DOE FROM Ledgers WHERE (InvstID = " & ID & " AND TTypeID = 1);"
    Set rs = DBEngine(0)(0).OpenRecordset(strSql)
    
    Do While Not rs.EOF
        Accumulator = Accumulator + CLng(rs!DOE)
        count = count + 1
        rs.MoveNext
    Loop
    
    PurchaseDate = CDate(Accumulator / count)
    
    rs.Close
    Set rs = Nothing
    End Function

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

Similar Threads

  1. Replies: 8
    Last Post: 11-04-2014, 10:44 PM
  2. DAvg Not Working
    By BLFOSTER in forum Forms
    Replies: 3
    Last Post: 04-21-2014, 11:14 AM
  3. Davg Multiple Criteria Syntax
    By JonathanT in forum Programming
    Replies: 1
    Last Post: 10-30-2013, 10:16 PM
  4. DAvg with multiple criteria help
    By mphynson44 in forum Access
    Replies: 4
    Last Post: 10-03-2013, 02:17 PM
  5. Problem with DAvg function
    By averagejoe in forum Access
    Replies: 1
    Last Post: 10-05-2012, 05:43 AM

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