regent
asked on
Place a totaled value into a TextBox
I'm using VB5. I've created a control array of 10 objects. I want to total the value in the 10 textboxes and place the value into a "sum" textbox. I start out putting default values into the control array (using a loop and making the value of each textbox equal i). Now I need to total the control array and have that total display in another textbox.
Also, I want the user to be able to enter values into the control array (textboxes). How can a make my "sum" textbox always equal the sum of the control array, i.e., not just upon form_load.
I think this'll be easy, I'm probably not asking it correctly. If necessary, I'll e-mail my form and it should make more sense. Thanks!
Also, I want the user to be able to enter values into the control array (textboxes). How can a make my "sum" textbox always equal the sum of the control array, i.e., not just upon form_load.
I think this'll be easy, I'm probably not asking it correctly. If necessary, I'll e-mail my form and it should make more sense. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dalin,
I get a type mismatch on my control array when I call the function. Will the function work with a control array, since VB says it's not really an array? Can I send you my form and have you look at it?
Tom
I get a type mismatch on my control array when I call the function. Will the function work with a control array, since VB says it's not really an array? Can I send you my form and have you look at it?
Tom
Send to me at Dalin_N@MailExcite.com
Sorry, I forgot you are passing a controler array. You need to revise a little in the function:
Private Function fGetSum(myControl As Object) As String
Dim i As Integer
Dim sum As Double
For i = myControl.Lbound To myControl.UBound
If IsNumeric(myControl(i).Tex t) Then
sum = sum + CDbl(myControl(i).Text)
Else
MsgBox "Invalid Entry", vbOKOnly, "Enter Numeric"
End If
Next i
fGetSum = CStr(sum)
End Function
To call it:
TxtSum = fGetSum(txtXVal)
where txtSum is the text box you want to display the sum, txtXVal is the textbox array.
let me know if there is any problems.
Private Function fGetSum(myControl As Object) As String
Dim i As Integer
Dim sum As Double
For i = myControl.Lbound To myControl.UBound
If IsNumeric(myControl(i).Tex
sum = sum + CDbl(myControl(i).Text)
Else
MsgBox "Invalid Entry", vbOKOnly, "Enter Numeric"
End If
Next i
fGetSum = CStr(sum)
End Function
To call it:
TxtSum = fGetSum(txtXVal)
where txtSum is the text box you want to display the sum, txtXVal is the textbox array.
let me know if there is any problems.
ASKER
tjr