Solved

Place a totaled value into a TextBox

Posted on 1998-10-11
5
201 Views
Last Modified: 2012-05-04
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!
0
Comment
Question by:regent
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
ID: 1439271
Regent:

First, add the function

Private Function fGetSum(myControl() as Textbox) as String
Dim i as Integer
Dim sum as Double

For i = myConTrol().Lbound to MyControl().UBound
   If IsNumeric(MyControl(i).Text) then
        Sum = Sum + CDbl(myControl(i).text)
       Else
        MsgBox "Invalid Entry", VBOKONLY, "Enter Numeric"
    End If
  Next i

fGetSum = cStr(Sum)
End Function

Now when you need to show the sum just add:
   TextSum = fGetSum(theTextArrayName())
You should properly replace TextSum with the name of the sum text box, , and TextArrayName with your 10 array name

To show the sum when the user is entering the number
 in the myTextArrayName_Change Event add:
If IsNumeric(myTextArrayName(Index)) then
       textSum = fGetSum (theTextArrayName())

Regards
Dalin

0
 

Author Comment

by:regent
ID: 1439272
Thanks for your help (again)!  :)

tjr
0
 

Author Comment

by:regent
ID: 1439273
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
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439274
Send to me at Dalin_N@MailExcite.com
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439275
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).Text) 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.



0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now