Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2006
    Posts
    7

    Get average of unbound textboxes.

    I'm new to Access forms and was wondering what the easiest way to get an average of unbound text boxes. There could be up to 30 values, but some of them will be blank so I won't want to count them in the average (If not isnull?) Thanks in advance for your help. I have been doing forms like this in vb.net, but my new company insists on doing this using Access forms.

  2. #2
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    I would try to do this in VBA, and apply a naming convention to the controls (like tb1, tb2, tb3, ..., tb30).

    Code:
    For Counter = 1 To 30
        Numerator = Numerator + _
            Val(Nz(Me.Controls("tb" & Counter).Value, 0))
        Denominator = IIf(Trim(Me.Controls("tb" & Counter).Value) = "", _
            0, 1)
    Next
    
    If Denominator = 0 Then
        'div by zero.  Do something
    Else
        Result = Numerator / Denominator
    End If

  3. #3
    Join Date
    Jul 2006
    Posts
    7
    Wow! That's fantastic Patrick, thanks. I didn't realize that I could use/call functions similar to what I was doing in VB.net. That code's going to save me tons of typing.

  4. #4
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    Glad to help!

    BTW, I noticed the calc for the denominator is bad. Here is corrected code:


    Code:
    For Counter = 1 To 30 
        Numerator = Numerator + _ 
            Val(Nz(Me.Controls("tb" & Counter).Value, 0)) 
        Denominator = Denominator + IIf(Trim(Me.Controls("tb" & _
            Counter).Value) = "", 0, 1) 
    Next 
    
    If Denominator = 0 Then 
        'div by zero.  Do something 
    Else 
        Result = Numerator / Denominator 
    End If

  5. #5
    Join Date
    Jul 2006
    Posts
    7
    Can you do a "numerator +=" and "denominator +=" instead of "num = num +" (abbr.)?

  6. #6
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    Quote Originally Posted by markhook
    Can you do a "numerator +=" and "denominator +=" instead of "num = num +" (abbr.)?
    No, VBA does not have that kind of operator.

  7. #7
    Join Date
    Jul 2006
    Posts
    7
    OK, thanks.

  8. #8
    Join Date
    Jul 2006
    Posts
    7
    Quote Originally Posted by matthewspatrick
    Glad to help!

    BTW, I noticed the calc for the denominator is bad. Here is corrected code:


    Code:
    For Counter = 1 To 30 
        Numerator = Numerator + _ 
            Val(Nz(Me.Controls("tb" & Counter).Value, 0)) 
        Denominator = Denominator + IIf(Trim(Me.Controls("tb" & _
            Counter).Value) = "", 0, 1) 
    Next 
    
    If Denominator = 0 Then 
        'div by zero.  Do something 
    Else 
        Result = Numerator / Denominator 
    End If
    The debugger does not like the me. Do I have to make this a class module? If so, how? Thanks.

  9. #9
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    I had assumed the code was going into the code module for your form. If not, replace "Me" with a reference to the form:

    Forms!MyForm

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

Similar Threads

  1. Saving unbound feild values
    By John Saul in forum Access
    Replies: 1
    Last Post: 06-19-2013, 06:22 AM
  2. Link unbound form to bound form
    By Papote in forum Forms
    Replies: 0
    Last Post: 09-25-2008, 07:42 PM
  3. Newbie Needs help with combine/average
    By wfd1753 in forum Queries
    Replies: 2
    Last Post: 05-16-2008, 06:43 AM
  4. Weighted Average - Challenges
    By edmund_xue in forum Access
    Replies: 0
    Last Post: 04-02-2008, 12:54 AM
  5. Replies: 0
    Last Post: 02-14-2007, 01:35 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