Link to home
Start Free TrialLog in
Avatar of jjc9809
jjc9809

asked on

Adding textbox columns using Visual Basic.Net Code

I have tried to add textbox columns in Visual Basic and I am getting an error when I use this coding:

 GDebit.Text = CType(Debit2.Text, Decimal) + CType(Debit3.Text, Decimal) + _
        CType(Debit4.Text, Decimal) + CType(Debit5.Text, Decimal) + CType(Debit6.Text) + CType(Debit7.Text) + _
        CType(Debit8.Text, Decimal)+ (CType(Debit9.Text, Decimal) + CType(Debit10.Text, Decimal) + CType(Debit11.Text, Decimal) + _
        CType(Debit12.Text, Decimal) + CType(Debit13.Text, Decimal) + CType(Debit14.Text, Decimal) + CType(Debit15.Text, Decimal) + _
        CType(Debit16.Text, Decimal) + CType(Debit17.Text, Decimal) + CType(Debit18.Text, Decimal) + CType(Debit19.Text, Decimal) + _
        CType(Debit20.Text, Decimal)))

GDebit.text is also a textbox.

How can I get GDebit to become the total of all of the textboxes added together.

I am using Visual Studio 2005.

jjc9809
Avatar of ajb2222
ajb2222

on the second line you forgot to convert the last two values to decimal

CType(Debit4.Text, Decimal) + CType(Debit5.Text, Decimal) + CType(Debit6.Text) + CType(Debit7.Text) + _

should be

CType(Debit4.Text, Decimal) + CType(Debit5.Text, Decimal) + CType(Debit6.Text, Decimal) + CType(Debit7.Text, Decimal) + _

Varios things...

I found too many parenthesis closing your sentence. Why?
I found CType(Debit6.Text) (you're CTyping to what type? type expected here)

Finally, I woud reccomend to make a function:

Function GetTotal() As Decimal
    Dim output As Decimal
    output += CType(Debit2.Text, Decimal)
    output += CType(Debit3.Text, Decimal)
    .... and so on
    Return output
End Function

And in your code:
GDebit.Text = GetTotal().ToString()

Very much clearer this way.

Hope that helps.
Avatar of jjc9809

ASKER

This function works, but what if you have no totals for Debit5.text and it is nothing coming in.  In other words, what if I have this coding:

I only have four numbers coming in for Debit1, 2, 3, 4, but Number 5 has nothing in it.

Function GetTotal() As Decimal
    Dim output As Decimal
    output += CType(Debit1.Text, Decimal)
    output += CType(Debit2.Text, Decimal)
    output += CType(Debit3.Text, Decimal)
    output += CType(Debit4.Text, Decimal)
    output += Ctype(Debit5.text, Decimal)

    .... and so on
    Return output
End Function

And in your code:
GDebit.Text = GetTotal().ToString()

To account for empty textboxes you need to check the length of the text in the textbox before trying to convert it to a decimal

Function GetTotal() As Decimal
    Dim output As Decimal
    if len(trim$(Debit2.Text)) > 0 then output += CType(Debit2.Text, Decimal)
    if len(trim$(Debit3.Text)) > 0 then output += CType(Debit3.Text, Decimal)
    .... and so on
    Return output
End Function
Avatar of jjc9809

ASKER

I finally  got the coding to work.  However, this coding works too.  I appreciate your help with this, but I have my Total coming in as 18000.00 and my other Total has 18,000.00.
I need to compare the two totals and give an error message when the two totals are out of Balance.
My coding to compare is:  If GDebit.Text <> Total.Debit.text Then
                                         Message.Show("Debit Column out of Balance")
                                         Exit Sub
                                         End If

The GDenot amount coming is is 18000.00 and comparing against 18,000.00.  So you going to get the error message since they are out of balance.  Howcan I convert the GDebit total of 18000.00 placing a comma inside the number.

I believe you can do this with the Length function, but I am unsure how to write it.
I know you have to count your spaces and insert the comma in the number somehow.

Any help here is appreciated too.
ASKER CERTIFIED SOLUTION
Avatar of ddancer99
ddancer99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can use
if FormatCurrency(GDebit.Text,2) <> FormatCurrency(Total.Debit.Text) Then
Message.Show("Debit Column out of Balance")
     Exit Sub
End If
Sorry, forgot to include the number of decimals in the last FormatCurrency

if FormatCurrency(GDebit.Text,2) <> FormatCurrency(Total.Debit.Text,2) Then
Message.Show("Debit Column out of Balance")
     Exit Sub
End If