Private Sub Average_Click()
Dim Test1 As Integer
Dim Test2 As Integer
Dim Test3 As Integer
Dim Test4 As Integer
Dim Test5 As Integer
Dim Test6 As Integer
Dim Average As String

Test1 = txtTest1.Text
Test2 = txtTest2.Text
Test3 = txtTest3.Text
Test4 = txtTest4.Text
Test5 = txtTest5.Text
Test6 = txtTest6.Text
Average = txtTotalAverage.Text


End Sub

Private Sub cmdClear_Click()
'clears all text boxes
txtTest1.Text = ""
txtTest2.Text = ""
txtTest3.Text = ""
txtTest4.Text = ""
txtTest5.Text = ""
txtTest6.Text = ""
txtTotalAverage.Text = ""

End Sub

Private Sub cmdCalculate_Click()
txtTotalAverage = (txtTest1.Text) + (txtTest2.Text) + (txtTest3.Text) + (txtTest4.Text) + (txtTest5.Text) + (txtTest6.Text) / 6

End Sub

Recommended Answers

All 3 Replies

Okay, in your sub Average_Click(), you declare the variables, then set the variables equal to that of a text box but then the sub ends and those variables go out of scope, which means they cannot be used any place else.

Also, in the future, when assigning variables that are numeric in nature to that of the contents of a text box, you will want to first check to see if the value in the text box is a numeric value. After that, you will want to convert the text value to the appropriate data type of the variable...

If IsNumeric(Text1.Text) = True Then 
  iValue1 = CInt(Text1.Text)
Else
  MsgBox "oops"
  Text1.SetFocus
  Exit Sub
End If

Now, do you see where this is going? No? Well I'll show you here in a bit but first, lets talk about operator precidence...

In visual basic, multiplication before division, multiplication and division before addition and subtraction, and addition before substraction, which means, your textbox # 6 is divided by 6 before being added with the rest of the values in the other textboxes. Which equates to...

6+6+6+6+6+6/6 = 33
6+6+6+6+6+1 = 33

So to change this, you would need to use parentheses to change the operator precedence. (This is just your basic math stuff...)

(6+6+6+6+6+6)/6=6

TextAverage.Text = (CInt(Text1.Text) + CInt(Text2.Text) + CInt(Text3.Text)) / 3

Good Luck

Okay, in your sub Average_Click(), you declare the variables, then set the variables equal to that of a text box but then the sub ends and those variables go out of scope, which means they cannot be used any place else.

Also, in the future, when assigning variables that are numeric in nature to that of the contents of a text box, you will want to first check to see if the value in the text box is a numeric value. After that, you will want to convert the text value to the appropriate data type of the variable...

If IsNumeric(Text1.Text) = True Then 
  iValue1 = CInt(Text1.Text)
Else
  MsgBox "oops"
  Text1.SetFocus
  Exit Sub
End If

Now, do you see where this is going? No? Well I'll show you here in a bit but first, lets talk about operator precidence...

In visual basic, multiplication before division, multiplication and division before addition and subtraction, and addition before substraction, which means, your textbox # 6 is divided by 6 before being added with the rest of the values in the other textboxes. Which equates to...

6+6+6+6+6+6/6 = 33
6+6+6+6+6+1 = 33

So to change this, you would need to use parentheses to change the operator precedence. (This is just your basic math stuff...)

(6+6+6+6+6+6)/6=6

TextAverage.Text = (CInt(Text1.Text) + CInt(Text2.Text) + CInt(Text3.Text)) / 3

Good Luck

hi thanks for that im getting the hang of it now ,at college in my first year so the coding is all new to me but that worked a dream many thanks stuhawk

Your welcome stuhawk, now if you don't mind, please mark this thread as solved...

Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.