Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21911

Finding Average of 3 Numbers excluding zeros

$
0
0
I am creating a application for my personal use. I have three textboxes in the form for inputting 3 numbers. I need to find the average of the three numbers entered in those textboxes.

Below are the scenario's in my application;

One textbox can have value and the other two will be left blank and value in single text box must be displayed as average

Two textbox can contain value and the other can be blank. Average for the Values in two textboxes must be displayed.

All the three text box can have value. Average for all the three values must be found.

I coded for this but it is not working properly. Below is my code for the reference.

Code:


Public Sub Calculate_Avg_Rate()
    If Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
        If Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
            txtAvgRice.Text = Val(txtRiceC.Text)
        End If
    ElseIf Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
        If Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
            txtAvgRice.Text = Val(txtRiceB.Text)
        End If
    ElseIf Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
        If Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
            txtAvgRice.Text = Val(txtValue1.Text)
        End If
    ElseIf Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
        txtAvgRice.Text = Round(((Val(txtRiceB.Text) + Val(txtRiceC.Text)) / 2), 2)
    ElseIf Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
        txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceC.Text)) / 2), 2)
    ElseIf Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
        txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceC.Text)) / 2), 2)
    Else
        txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceB.Text) + Val(txtRiceC.Text)) / 3), 2)
    End If
End Sub

I am calling the above procedure in the fourth textbox got focus event. The fourth textbox will display the average value when it is getting clicked.

When I enter the value in the textboxes, the average is not getting populated. I have also attached the project for reference.

Any help regarding is highly appreciated. Thanks in advance.



--------------------------------------------------------------------------------

Thanks you and Kind Regards, Karthik Venkatraman
Attached Files

Viewing all articles
Browse latest Browse all 21911


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>