Results 1 to 17 of 17

Thread: how to format textbox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253

    how to format textbox?

    hi

    i am having problem in formating textbox. i have set textbox dataformat to NUMBER. i add number entered in 3 textvoxes and display the total in 4th textbox. if the input in the textboxes are.....12,345,12,345,12,345 (addtion is done in textbox lost focus events). it displays the total in 4th textbox as only 36 instead of 37,035.

    how can i solve the problem?

    thanks

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Post your code then we can understand you better. Did my solution here (http://www.vbforums.com/showthread.p...ghlight=danial) helped or you are looking for something different.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I assume you are using Val. That won't work since it will truncate everything after the first non-number and in considers "," to be a non-number. Do this instead.

    CDbl(Text1.Text) + CDbl(Text2.Text) + CDbl(Text2.Text)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253
    hi danial i have almost 100 textboxes and that was a tedious job to write the replace code for each n every textbox, that's why i removed the dataformat. but i know i think i should do it for end user. Any other easy way u can suggest me?


    hi Martin

    i had tried this before and i tried it again now, but i got an error (type mismatch) the moment i leave the 1st textbox. below are 2 codes which i tried.:

    Code:
    Private Sub totSHF()
    txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
    End Sub
    ------------------------------------------------------------------------
    Private Sub totSHF()
    CDBL(txtFields(16).Text) = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
    End Sub

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Type mismatch, one of your textboxes probably has invalid data (zero length string or non numeric)

    Before you use those equations, validate your textbox.text replacing "" with zero.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by leinad31
    Type mismatch, one of your textboxes probably has invalid data (zero length string or non numeric)

    Before you use those equations, validate your textbox.text replacing "" with zero.
    SillyLady: Did leinad31's suggestion work for you? It should work if you use the first of the two totSHF subs above. BTW, do you know about line continuations.

    VB Code:
    1. Private Sub totSHF()
    2. txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) _
    3.                    + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) _
    4.                    + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) _
    5.                    + CDbl(txtFields(14)) + CDbl(txtFields(15))
    6. End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253
    ok guys your suggestions seems to be working. my code add the numbers the moment each textbox loses the focus. problem is when i 1st textbox loses the focus the addition function called and it's taking the other blank textboxes also that's why it shows mismatch error. there may be a situation where user did not enter anything not even Zero in a textbox.
    I tried the code to check if the user has input anything, if not then code will put Zero in a textbox. even this is not working. i am pasting code here:

    VB Code:
    1. Private Sub txtFields_LostFocus(Index As Integer)
    2.  
    3. If txtFields(Index).Text = "" Then
    4. txtFields(Index).Text = 0
    5. Exit Sub
    6. Else
    7. txtChange (Index)        ' This is the function to add numbers
    8. End If
    9.  
    10. End Sub

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    One way to fix the problem would be to set the value of each textbox to 0 in the form's Load event. It would be better however if you posted the txtChange sub (a very bad name BTW) so that I can see what you are doing.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253

    Smile

    here is the code.

    VB Code:
    1. Private Sub txtChange(Index1 As Integer)
    2. Select Case Index1
    3. Case 5
    4.     totSHF
    5. Case 6
    6.     totSHF
    7. Case 7
    8.     totSHF
    9. Case 8
    10.     totSHF
    11. Case 9
    12.     totSHF
    13. Case 10
    14.     totSHF
    15. Case 11
    16.     totSHF
    17. Case 12
    18.     totSHF
    19. Case 13
    20.     totSHF
    21. Case 14
    22.     totSHF
    23. Case 15
    24.     totSHF
    25. End Select
    26. End Sub
    27.  
    28. Private Sub totSHF()
    29. txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
    30. End Sub

    Martin i am very new to VB and silly also.

  10. #10
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    VB Code:
    1. Private Sub totSHF()
    2. txtFields(16).Text = 0 'Just added this line
    3.     For i = 5 To 15
    4.         If txtFields(i).Text <> "" Then
    5.             txtFields(16).Text = CDbl(txtFields(16).Text + CDbl(txtFields(i).Text)) ' Change this line
    6.         End If
    7.     Next
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11.     For i = 5 To 15
    12.         txtFields(i).Text = 0
    13.     Next
    14. End Sub
    15.  
    16. Private Sub txtFields_LostFocus(Index As Integer)
    17.     Select Case Index
    18.         Case 5 To 15
    19.             totSHF
    20.     End Select
    21.  
    22. End Sub

    This helps?

    [edit]See comments above[/edit]
    Last edited by veryjonny; Feb 18th, 2004 at 02:46 AM.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253
    hi all thanks for the all the suggestions. i took the cue from johny example and made a simple change in my code. i displayed the Zero in all the text box when user click add and this is working so far, hope there will be no error any more.

    thank u guys for all ur help.

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Let's talk about this sub for a minute.

    VB Code:
    1. Private Sub txtChange(Index1 As Integer)
    2. Select Case Index1
    3. Case 5
    4.     totSHF
    5. Case 6
    6.     totSHF
    7. Case 7
    8.     totSHF
    9. Case 8
    10.     totSHF
    11. Case 9
    12.     totSHF
    13. Case 10
    14.     totSHF
    15. Case 11
    16.     totSHF
    17. Case 12
    18.     totSHF
    19. Case 13
    20.     totSHF
    21. Case 14
    22.     totSHF
    23. Case 15
    24.     totSHF
    25. End Select
    26. End Sub
    The reason I said it was a bad name is that you named it just like you would name a textbox and someone looking at your code would be very confused by your txtChange (Index) line. You did a good thing when you added the comment to that line. It would have been better however if you gave the sub some more standard name such as maybe GatherData.

    The code in that sub is also unnecessarily long and you could change it to
    VB Code:
    1. Private Sub txtChange(Index1 As Integer)
    2. Select Case Index1
    3. Case 5 To 15
    4.     totSHF
    5. End Select
    6. End Sub

    Having said all that, I need to point out that the sub really does nothing and is unnecessary. What you are currently doing is passing the index number of your txtFields control array to the txtChange sub. (You are not passing the data that in the particular txtFields, just the index to the txtFields field). Then no matter what the index number is, you are always doing the same thing and that is calling totSHF. So why not just call totSHF directly with this code.
    VB Code:
    1. Private Sub txtFields_LostFocus(Index As Integer)
    2.  
    3.     If txtFields(Index).Text = "" Then
    4.         txtFields(Index).Text = 0
    5.     Else
    6.         totSHF
    7.     End If
    8.  
    9. End Sub
    Note the indentation and the removal of the unnecessary Exit Sub.

  13. #13
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Then no matter what the index number is, you are always doing the same thing and that is calling totSHF. So why not just call totSHF directly with this code.
    No Martin, shes checking if the Index No falls between 5 and 15 then shes calling that sub.

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by veryjonny
    No Martin, shes checking if the Index No falls between 5 and 15 then shes calling that sub.
    Well her totSHF sub only adds textboxes 5 to 15, so if the index was less than 5 or more than 15 it would do nothing anyhow.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Posts
    253
    hi all

    Martin as i told you i am very new to VB that's why u see some silly mistakes. You are right to say that I should call the totSHF the way you have suggested. Actually there are many more function to be called at different textbox.

    Anyways you guys have really help me alot in clearing my concept.

    Thanks

  16. #16
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Everyone makes mistake, no matter how experienced he/she is.

    The best part is that we shd learn from our mistakes.

  17. #17
    Banned
    Join Date
    Jul 2003
    Location
    New delhi
    Posts
    143
    simple codin in two line


    Join all the textbox store in a string



    then

    textbox4.text = format(value,"@@,@@@,@@@@@")



    simple slove in two line


    hahahahahahahahahahhahahahahahhah

Posting Permissions

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



Click Here to Expand Forum to Full Width