Results 1 to 4 of 4

Thread: Calculation not working correctly

  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2013
    Posts
    9

    Calculation not working correctly

    Could somebody please help me out with getting my calculation to work. What I am trying to do is, if textbox31 is a negative number then it needs to subject textbox31 from textbox1 to get a bigger negative number. If textbox 31 is a positive number then it needs to subtract textbox 1 from textbox31 to come come up with the answer which should be positive. I have tried the following code but it seems to only be doing the first part of the equation and not the second ie the answer is always negative. Some help with this would be greatly appreciated. Diane

    If TextBox31.Text < 0 Then
    Form1.TextBox1.Text = Val(TextBox1.Text) - Val(TextBox31.Text)
    Else

    Form1.TextBox1.Text = Val(TextBox31.Text) - Val(TextBox1.Text)
    EndIf


    I have also tried: If Val(TextBox31.Text < 0) Then
    but the result is the same

  2. #2
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Calculation not working correctly

    if you mean the length of text in textbox31 then
    Code:
    If Len(Textbox31.text) <= 0 then
    
    'your code
    End If
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  3. #3
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Calculation not working correctly

    Try this:

    Code:
     If CInt(TextBox31.Text) < 0 Then
    
                Dim result As Integer = CInt(TextBox31.Text) - CInt(TextBox1.Text)
                FormatNumber(result.ToString("n"))
                TextBox1.Text = result
            Else
                Dim result As Integer = CInt(TextBox31.Text) - CInt(TextBox1.Text)
                FormatNumber(result.ToString("n"))
                TextBox1.Text = result
            End If
    follow your math closely

    -50 minus 50 = -100
    -50 plus 50 = 0

    Not sure the exact result you were looking for hope this helps you though

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Calculation not working correctly

    Er, simple algebra. The calculation isn't wrong. Your expectation of it is.

    x -(-y) = x + y which by definition can't be a 'bigger negative number'. You need x +(-y) to make the equation work BUT it will still only be a 'bigger negative number' if x is negative.So with your present equations ...

    TB1 = 5 and TB31 = -10 : 5 -(-10) = 15
    TB1 = 5 and TB31 = 10 : 10 - 5 = 5
    TB1 = -5 and TB31 = -10 : -5 -(-10) = 5
    TB1 = -5 and TB31 = 10 : 10 -(-5) = 15
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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