Results 1 to 7 of 7

Thread: Stupidly easy question

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    BACKGROUND : Teaching brother vb, starting with 3 txtboxes on a form simple calculator example (4 buttons add, minus, divide, multiply). type number into txt1 & txt2, hit one of the 4 buttons, hey presto, total in box3.

    PROBLEM :
    OK, I use
    Code:
    txt3.text = txt1.text * txt2.text
    which works fine with * & /. With the + & - however I get the following:

    • ON the -, no entries appear
    • On the +, 2 numbers join, not add (I.e.- 1+1=11, 2+2=22)


    Now even I know 2+2 is not 22, this just doesn't add up (pun intended there, sorry, had to put it in). Can anyone shed some light on this please?

    Thank you!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Junior Member
    Join Date
    Aug 2000
    Location
    Oslo, Norway
    Posts
    22
    I believe the problem is that you are using strings to do math. What you should do is to put your numbers in variables ex. integer, long etc.

    lars
    Lars Jarle Mæhlum
    http://www.touch-it.no

  3. #3
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Cool Think of the Data Type

    Convert the Text into a numeric data type

    either Integer, Single, Double, Byte etc..

    txt3.text = CInt(txt1.text) * CInt(txt2.text)

    The reason for getting 22 and 11 is because it is concatenating two strings

    Hope that helps
    Steve

  4. #4
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138

    Convert

    Convert ! And use the right operators. Don't use & to add because it will concatenate the strings.
    Do something like (I didn't test this, wrote it straight into the reply):

    Code:
    dim dblValue1 as double
    dim dblValue2 as double
    
        if IsNumeric(txt1) then
           dblValue1 = cdbl(txt1)
    
           if IsNumeric(txt2) then
              dblValue2 = cdbl(txt2)
              
              txt3 = cstr(dblValue1 + dblValue2)
           Else
              msgbox "Non-numeric value
           End
         Else
           msgbox "Non-numeric value
         EndIf
    Don't rely on VB to convert values properly. It will often go well, but it will go wrong when you least expect it.
    Regards,
    Laurens

    Using VB5 Enterprise edition SP3
    VB6 Enterprise edition SP5

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well A + B = AB. When you add text you're concatenatin strings. Textboxes have text in the text property (yeah, no kidding)
    What you want to do is to convert the text into number values.
    Code:
    txt3.Text = Val(txt1.Text) + Val(txt2.Text)
    Good luck!

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Wow, I've never seen so many people answering a question at virtually the same time.
    Just one comments though;
    Don't use CInt if you're not only calculation integers.

  7. #7

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Thank you everyone!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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