Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 33

Thread: Solved: Adding Txt Boxes in Form

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: Adding Txt Boxes in Form

    I have about 10 txtboxes in a UserForm. I have a LblTotal that I would like to have updated everytime there is a change in those txtboxes. How can I do that?

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    What do you want totalled and where if you want LblTotal to be the combined total of the textboxes maybe Me.LblTotal=textbox1.value + Textbox2.value....etc
    or do you want to count the amount of controls?
    Me.Controls.Count?

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Well the txtboxes would just be a number. So I am trying
    Me.LblTotal.Caption = TxtQua1.Value + TxtQua2.Value + TxtQua3.Value + TxtQua4.Value + TxtQua5.Value + TxtQua6.Value + TxtQua7.Value + TxtQua8.Value + TxtQua9.Value + TxtQua10.Value + TxtQua11.Value + TxtQua12.Value

    But that's not working its not giving me anything.
    Where should I put that?

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Not sure Em but I think you have to dim the textbox variables as integers...this works but I only did the first 2 textboxes for you....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    That adds them but I would like for that to happen automatically not have to click a cmd button. Can that be done?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use the textbox exit procedure.

  7. #7
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Would I need that in each textbox?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yep.

  9. #9
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Instead of it giving me a sum it's giving me 1111
    If the first 4 are 1's.
    How can I assign txtboxes to be integers?

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Add them, don't concatenate.

  11. #11
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    please explain

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use CDbl(Texbox1.Text) + CDbl(TextBox2.Text) + ...

    instead of

    TextBox1.Text + TextBox2.text + ...

  13. #13
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    The Textbox1 + Textbox2....is my fault Bob, i forgot about one of your answers to earlier posts where you Cast Double.

    Apologies!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You would think that it would be smart enough to know that + means add, and automatically Cast it for you. After all, there is a & operator to allow true concatenation.

  15. #15
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Well when i wrote "+" that was my assumption because with what little i know, i know that "&" means this AND that. All i can imagine Bob is that it must have been a human that devised the language and had he or she made it perfect there would be no room for improvement and no Excel 2007 etc!
    LOL!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    and then where would MS be ?

  17. #17
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I would imagine Bill Gates would still be drawing on his UB40!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  18. #18
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    How can I have that work without having to click anything. I have cmdbuttons that will autopopulate some of the TxtQua Boxes.

  19. #19
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Em,
    I would add a sub to the userform like this:
    [vba]Private Sub add()
    LblTotal.Caption = CDbl(TxtQua1.Text) + CDbl(TxtQua2.Text) _
    + CDbl(TxtQua3.Text) + CDbl(TxtQua4.Text) + CDbl(TxtQua5.Text)
    End Sub

    [/vba]
    then in each command button add a call to the add routine like this:
    [vba]Private Sub CmdNC8430_Click()
    LoadTextBoxes "HP NC8430", "", "1"
    LoadTextBoxes "LAPTOP BAG", "Expense", "1"
    LoadTextBoxes "KEYBOARD", "Expense", "1"
    LoadTextBoxes "MOUSE", "Expense", "1"
    LoadTextBoxes "PORT REPLICATOR", "Expense", "1"
    add
    End Sub[/vba]

    I only did the one button for you but you can get it from there I'm sure
    CmdNC8430_Click() is the one I set up for you in the attachment. You will have to add the call to "add" to each commandbutton
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  20. #20
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Hey lucas that seems to work good only thing is it's giving me an error. It seems the reason it's giving me an error is because the other values are "". How can I fix that.

    I have this
    Private Sub add()
    UserForm1.LblTotal.Caption = CDbl(TxtQua1.Text) + CDbl(TxtQua2.Text) + CDbl(TxtQua3.Text) + CDbl(TxtQua4.Text) + CDbl(TxtQua5.Text) + CDbl(TxtQua6.Text) '+ CDbl(TxtQua7.Text) + CDbl(TxtQua8.Text) + CDbl(TxtQua9.Text) + CDbl(TxtQua10.Text) + CDbl(TxtQua11.Text) + CDbl(TxtQua12.Text)
    End Sub

    So if a cmdbutton is only 4 or 5 lines TxtQua6.Text through TxtQua12.Text = "". Any way i can possibly do an if statement to test if it's "" if so then ignore

Posting Permissions

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