Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding textbox columns using Visual Basic.Net Code

Posted on 2010-09-23
8
Medium Priority
?
581 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:jjc9809
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 33744574
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) + _

0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 33744597
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.
0
 

Author Comment

by:jjc9809
ID: 33745288
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()

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:ddancer99
ID: 33746585
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
0
 

Author Comment

by:jjc9809
ID: 33747046
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.
0
 
LVL 3

Accepted Solution

by:
ddancer99 earned 2000 total points
ID: 33747855
try this
if CDBL(GDebit.Text) <> CDBL(Total.Debit.Text) Then
     Message.Show("Debit Column out of Balance")
     Exit Sub
End If
0
 
LVL 3

Expert Comment

by:ddancer99
ID: 33747869
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
0
 
LVL 3

Expert Comment

by:ddancer99
ID: 33747878
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
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 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