+ Reply to Thread
Results 1 to 9 of 9

Simple textbox question

  1. #1
    Registered User
    Join Date
    02-15-2006
    Posts
    93

    Simple textbox question

    Good morning,

    I think this is a simple question but I am not sure how to handle it correctly. I have two textboxes on a userform. The values are numeric and I want to add them and display the results in the caption of a label.

    Basic setup that I tried:
    Sub Test()
    Label1.caption = Textbox1.value + Textbox2.value
    End Sub

    If I entered 5 into textbox1 and 2 into textbox2, the result that is diplayed in the caption of label1 is 52 instead of 7. If I multiply each textbox by 1, it works correctly but I don't think this is the best way to handle this.

    Any advice?
    Thanks in advance,
    dw

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    try

    label1.caption = cstr(Val(textbox1.Value) + Val(textbox2.Value))

  3. #3
    Registered User
    Join Date
    02-15-2006
    Posts
    93

    Question

    Thanks Tony,

    That does work but I seem to be complicating it a little bit... wondering if you can help me out some more. I want the first textbox value to appear with a $. So the code that I tried is:

    Private Sub Textbox1_change()
    Label1.caption = cstr(Val(textbox1.Value) + Val(textbox2.Value))
    Textbox1.text = format(Textbox1.value, "#,##0")
    End sub

    Private Sub Textbox2_change()
    Label1.caption = cstr(Val(textbox1.Value) + Val(textbox2.Value))
    Textbox2.text = format(Textbox2.value, "#,##0")
    End sub

    I am still having trouble with the above code. For example, if I had 20,000 in textbox 1 and 30,000 in textbox 2, Label1 displays 50 instead of 50,000. Any ideas on how I fix this? Ideally, I would like to format textbox 1 as "$#,##0" but this really doesn't work with the code I have here.

    Thanks again for any help!
    dw
    Last edited by toocold; 11-06-2006 at 10:35 PM.

  4. #4
    Registered User
    Join Date
    02-15-2006
    Posts
    93
    Still looking for any ideas on this. I tried using CCur but this didn't work either.

    Ideas?

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,487
    Label1 = Application.Text(CStr(Val(TextBox1.Value) + Val(TextBox2.Value)), "$#,##0.00")

  6. #6
    Registered User
    Join Date
    02-15-2006
    Posts
    93
    Hi Dave,

    Thanks for the response. That displays the caption in the correct format but it still doesn't solve the problem I am having with the formatted textboxes.

    If the textbox is formatted with any characters, including the "," the val function only recognizes the numbers up until that sign. So if I use the format "#,##0" for the textbox, then the caption only displays 50 if textbox1 had 20,000 in it and textbox2 had 30,000 in it. It drops the "000" after the commas. I want to display the full amount because a person could enter 20,500 in textbox1 and this would not show up.

    Any other ideas?
    Cheers,
    dw

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,487
    Hi DW,

    After some extensive investigating I have come to the conclusion that this is not easily performed, it looks like you will have one or the other but not both, I hope I am wrong because, it really doesn't make sense that you can format your entries in the textbox, but not be able to add the textboxes together after,

    the following code appears to work only in the textbox exit, if you used it in the textbox change, it activates to fast and you wont be able to finish typing in the number

    Good luck and if you find an answer to adding the two boxes together let us know

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-15-2006
    Posts
    93
    Hi Dave,

    Thanks for taking the time to try and work this out for me! I will keep looking for the solution but in the meantime, I will try your idea out.
    Cheers,
    dw

  9. #9
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    An improvement is to store the textbox value in a variant add them together and use the result. This doesn't overcome the currency symbol but does overcome the number format characters

    Private Sub CommandButton1_Click()

    Dim v1 As Variant
    Dim v2 As Variant
    v1 = txt1.Value
    v2 = txt2.Value
    txt3=v1+v2*1.0

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1