Results 1 to 21 of 21

Thread: [RESOLVED] I can't sum demical numbers...(vb6)

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Resolved [RESOLVED] I can't sum demical numbers...(vb6)

    hi guys!

    i have put this code in my project.

    Code:
    Private Sub Command1_Click()
    Form1.Textx = Val(Form1.Textx) + Val(Form2.Textz)
    End Sub
    i cant sum demical numbers but only integer numbers...

    What am i doing wrong? Could u tell me pleaze?!

  2. #2
    Member
    Join Date
    Feb 2008
    Posts
    49

    Re: I can't sum demical numbers...(vb6)

    The problem might be with the definition of the textx and textz properties.
    What are they?

    Peter Schoots

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by Peter Schoots View Post
    The problem might be with the definition of the textx and textz properties.
    What are they?

    Peter Schoots

    Just simple text boxes!i didn't do anything with their properties...

  4. #4
    Member
    Join Date
    Feb 2008
    Posts
    49

    Re: I can't sum demical numbers...(vb6)

    Text1.Text = Val(Text1) + Val(Text2)

    Textx and textz being textboxes it should work if the strings have decimal points in it. I tried the above statement which works. What are the values in the textz and textx boxes?

  5. #5
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,697

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by Sup3r View Post
    hi guys!

    i have put this code in my project.

    Code:
    Private Sub Command1_Click()
    Form1.Textx = Val(Form1.Textx) + Val(Form2.Textz)
    End Sub
    i cant sum demical numbers but only integer numbers...

    What am i doing wrong? Could u tell me pleaze?!

    Caution! Try this code in Immedate window:
    Code:
    ?Val("123,4")
     123 
    
    ?Val("123.4")
     123,4 
    
    ?Val("123,4") + Val("123,4")
     246 
    
    ?Val("123.4") + Val("123.4")
     246,8

  6. #6
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    Try this

    vb Code:
    1. Private Sub Command1_Click()
    2. Form1.Textx = CDbl(Val(Form1.Textx)) + CDbl(Val(Form2.Textz))
    3. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  7. #7
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,697

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by koolsid View Post
    vb Code:
    1. Private Sub Command1_Click()
    2. Form1.Textx = CDbl(Val(Form1.Textx)) + CDbl(Val(Form2.Textz))
    3. End Sub
    Same thing, same result:

    Code:
    ?Val("123.4") + Val("123.4")
     246,8 
    ?CDbl(Val("123.4")) + CDbl(Val("123.4"))
     246,8
    
    ?Val("123,4") + Val("123,4")
     246 
    ?CDbl(Val("123,4")) + CDbl(Val("123,4"))
     246

  8. #8
    New Member
    Join Date
    Oct 2009
    Location
    Roma Italy
    Posts
    9

    Re: I can't sum demical numbers...(vb6)

    You can use the following function that makes use of CDbl and you solve the problem in all forms. Remember to write the Text (Form2.Textz.Text) property.

    Code:
    Option Explicit
    '----------------------------------
    Public Function Ctr(strTesto As String) As Double
        On Error Resume Next
            
        If IsNumeric(strTesto) = True Then
            Ctr = CDbl((strTesto))
            Else: Ctr = 0
        End If
        
    End Function
    '----------------------------------
    Sub Command1_Click()
        Form1.Textx.Text = Ctr(Form1.Textx.Text) + Ctr(Form2.Textz.Text)
    End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Still nothing... I am not confused with (,) or (.) and this code

    Code:
    Form1.Textx = CDbl(Val(Form1.Textx)) + CDbl(Val(Form2.Textz))
    sums correct only the first time that i am putting a demical number in the textbox! Then the result is not right! ;/

    Any other ideas?

  10. #10
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    Works for me...
    Attached Images Attached Images  
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by LouisItaly View Post
    You can use the following function that makes use of CDbl and you solve the problem in all forms. Remember to write the Text (Form2.Textz.Text) property.

    Code:
    Option Explicit
    '----------------------------------
    Public Function Ctr(strTesto As String) As Double
        On Error Resume Next
            
        If IsNumeric(strTesto) = True Then
            Ctr = CDbl((strTesto))
            Else: Ctr = 0
        End If
        
    End Function
    '----------------------------------
    Sub Command1_Click()
        Form1.Textx.Text = Ctr(Form1.Textx.Text) + Ctr(Form2.Textz.Text)
    End Sub
    LouisItaly that is working perfect but i have not enable the (,) button in the text box... The user can type only the (.) button for demical point...and this is not working with (.) button! Can u make your magics pleaze??? hahahaha!

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by koolsid View Post
    Works for me...

    I have 2 forms in my project.Thats maybe why is not working to my project.

  13. #13
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    Can u upload u r project?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  14. #14
    Member
    Join Date
    Feb 2008
    Posts
    49

    Re: I can't sum demical numbers...(vb6)

    I don't think you need dbl(val(textx.text)). dbl(textx.text) will do.

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by koolsid View Post
    Can u upload u r project?
    Sure! here it is!

    http://rapidshare.com/files/29775960..._____.rar.html

    Check it out!

  16. #16
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    The link tells me

    This file can only be downloaded by becoming a Premium member.

    There are no more download slots available for free users right now. If you don't want to become a premium member, you might want to try again later.

    Can you upload a zip file or mail it to me?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: I can't sum demical numbers...(vb6)

    Quote Originally Posted by koolsid View Post
    The link tells me

    This file can only be downloaded by becoming a Premium member.

    There are no more download slots available for free users right now. If you don't want to become a premium member, you might want to try again later.

    Can you upload a zip file or mail it to me?

    Sorry in my panic i forgot that i have a rapidshare premium account...

    Here it is again...

    http://rapidshare.com/files/29777216...__________.rar

    Thanks a lot!!!

  18. #18
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    Seems like you panicked again....

    I am getting the same message again...

    I have pm'd you my email address. you can mail it to me...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  19. #19
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: I can't sum demical numbers...(vb6)

    As discussed the problem is not the code but the regional settings... the decimal was not being recognized since you had Greece setting on.... Change it to English US from "Regional and language Options" and it will work fine.... as we tested it...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  20. #20
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [RESOLVED] I can't sum demical numbers...(vb6)

    You can do some tricks to make your application to be compatible with any regional settings. The only thing you have to do is replace the commas.

    vb Code:
    1. Dim A As String, B As String, D As Double
    2.   Dim sFormatComma As String
    3.   A = "10,0006"
    4.   B = "2.345"
    5.  
    6.   sFormatComma = Left$(Format$(CStr(1 / 2), ".0"), 1) 'lets see what the Format$ will gives you for the decimal sign
    7.  
    8.   If sFormatComma = "," Then
    9.    'convert comma to dot
    10.     A = Replace$(A, ",", ".")
    11.     B = Replace$(B, ",", ".")
    12.   End If
    13.  
    14.   D = val(A) + val(B)
    15.   Debug.Print D

    There might be other ways to do this, but it is definitely does the job.
    Last edited by Jim Davis; Oct 25th, 2009 at 02:31 PM.

  21. #21

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    33

    Re: [RESOLVED] I can't sum demical numbers...(vb6)

    Quote Originally Posted by Jim Davis View Post
    You can do some tricks to make your application to be compatible with any regional settings. The only thing you have to do is replace the commas.

    vb Code:
    1. Dim A As String, B As String, D As Double
    2.   Dim sFormatComma As String
    3.   A = "10,0006"
    4.   B = "2.345"
    5.  
    6.   sFormatComma = Left$(Format$(CStr(1 / 2), ".0"), 1) 'lets see what the Format$ will gives you for the decimal sign
    7.  
    8.   If sFormatComma = "," Then
    9.    'convert comma to dot
    10.     A = Replace$(A, ",", ".")
    11.     B = Replace$(B, ",", ".")
    12.   End If
    13.  
    14.   D = val(A) + val(B)
    15.   Debug.Print D

    There might be other ways to do this, but it is definitely does the job.
    Thank u too Jim Davis! I 'll keep that to my mind!

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