Solved

Adding textbox columns using Visual Basic.Net Code

Posted on 2010-09-23
8
574 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…

717 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