Solved

Adding textbox columns using Visual Basic.Net Code

Posted on 2010-09-23
8
571 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export Data to Different .csv Files 26 103
VB6 - Scroll Mouse wheel on Picturebox 13 42
Sql server insert 13 26
Help with exporting to excel 4 25
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 …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

816 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

9 Experts available now in Live!

Get 1:1 Help Now