Results 1 to 4 of 4
  1. #1
    RunTime91 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2014
    Posts
    284

    Help with Calculated Control

    Greetings All...

    I am struggling with setting up a Main Form (not a sub-form) TextBox control (TxtSysAvg) to calculate the average of data entered into 3 other TextBoxes.
    The 3 textboxes that are being averaged are not tabled, and I would rather not have to create a table for them... The Average Measurement will be tabled.



    Needless to say, I've tried several iterations of the code below - And I get nuthin but errors.

    Code:
    Private Sub TxtSys3_AfterUpdate()
    Me.TxtSysAvg = Avg(Me.TxtSys1 + Me.TxtSys2 + Me.TxtSys3)
    End Sub
    Any help, as always, is greatly appreciated.

    RT91

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    You cannot use avg in that way

    you need to add the 3 values and divide by 3. You will get errors until all three controls have valid values unless you use the nz function around each control name

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I agree with CJ.

    You might consider code along these lines.
    eg. I used Form23 to test.
    Code:
    Private Sub Form23_AfterUpdate()
        Call CalculateAverage
    End Sub
    Code:
    Private Sub TAVG_Click()
    'event to force the calculation and display of the Average in TAvg
     Call CalculateAverage
    End Sub
    Code:
    Private Sub t1_AfterUpdate()
    'process avg after each textbox is valued
        Call CalculateAverage
    End Sub
    
    Private Sub t2_AfterUpdate()
    'process avg after each textbox is valued
        Call CalculateAverage
    End Sub
     
    Private Sub t3_AfterUpdate()
    'process avg after each textbox is valued
        Call CalculateAverage
    End Sub
    Code:
    Private Sub CalculateAverage()
       ' Identify the variables
        Dim val1 As Double
        Dim val2 As Double
        Dim val3 As Double
        Dim avg As Double
    
        ' Check if the textboxes have valid numeric values; otherwise set to 0
        If IsNumeric(Me.T1) Then val1 = CDbl(Me.T1) Else val1 = 0
        If IsNumeric(Me.T2) Then val2 = CDbl(Me.T2) Else val2 = 0
        If IsNumeric(Me.T3) Then val3 = CDbl(Me.T3) Else val3 = 0
    
        ' Calculate the average of the values in the textboxes
        avg = (val1 + val2 + val3) / 3
    
        ' Set the value of the TAvg textbox
        Me.TAVG = avg
    End Sub
    Last edited by orange; 01-12-2025 at 05:37 PM. Reason: spelling

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Simple expression in textbox will serve. Until all 3 have data, the result will show Null, shouldn't show error. =(TxtSys1 + TxtSys2 + TxtSys3)/3

    Saving calculated value to table is usually not necessary and can be dangerous. Could use Calculated type field in table.
    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. Replies: 3
    Last Post: 11-30-2016, 07:18 PM
  2. Replies: 4
    Last Post: 02-18-2016, 12:06 PM
  3. Replies: 12
    Last Post: 10-01-2013, 12:59 PM
  4. calculated field from calculated field?
    By RedGoneWILD in forum Reports
    Replies: 5
    Last Post: 08-03-2010, 02:32 PM
  5. Calculated control help
    By cici in forum Forms
    Replies: 4
    Last Post: 05-16-2010, 12:04 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