Solved

Adding textbox columns using Visual Basic.Net Code

Posted on 2010-09-23
8
570 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:ddancer99
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:jjc9809
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 28
Sending Text to device in .NET 9 30
Vb.Net Menu 1 20
Create XML 5 30
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now