11 Replies - 611 Views - Last Post: 05 September 2012 - 09:58 AM Rate Topic: -----

#1 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Problem with adding in VB

Posted 05 September 2012 - 08:43 AM

Hi. I am a complete newbie at VB 6 and have the interest in learning. Found a sample code and have modified it for my use for what i am doing. I need to get a sum of 4 text boxes which have to be enabled. Below is the code I have modified, but when I push F5, I get the following error message.

Private Sub label45_click()
Dim a, b, c, d, r As Integer

a = Val(Text49.Text)
b = Val(Text50.Text)
c = Val(Text51.Text)
d = Val(Text52.Text)
'r = a + b + c + d
txtTotalCost.Text = r

    'Check which textbox is selected to perform sum
    If Text49.Enabled = True Then
        r = a
        txtTotatlCost.Text = r
    Else
        If Text49.Enabled = True And Text50.Enabled = True Then
            r = a + b
            txtTotalCost.Text = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True And Text51.Enabled = True Then
            r = a + b + c
            txtTotalCost.Text = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True And Text51.Enabled = True And Text52.Enabled = True Then
            r = a + b + c + d
            txtTotalCost.Text = r
        End If
    End If

End Sub



I would like some advise that could resolve this.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with adding in VB

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 08:50 AM

It's pretty straight forward. You are trying to call a variable and you never declared it. Probably a mistype or what not. This is a super simple problem you can do with or without debugging. Your pick.
Was This Post Helpful? 0
  • +
  • -

#3 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Re: Problem with adding in VB

Posted 05 September 2012 - 08:56 AM

Cool. I was able to to see that the txtTotalCost.text was not declared. That was fairly simple :D.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 08:57 AM

Sometimes those error messages aren't out to get'cha.
Was This Post Helpful? 0
  • +
  • -

#5 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Re: Problem with adding in VB

Posted 05 September 2012 - 09:19 AM

I was able to run debug to test. But for some reason it does not add the numbers in random selection.

Private Sub label45_click()
Dim a, b, c, d, r As Long

a = Val(Text49.Text)
b = Val(Text50.Text)
c = Val(Text51.Text)
d = Val(Text52.Text)
'r = a + b + c + d

    'Check which textbox is selected to perform sum
    If Text49.Enabled = True Then
        r = a
        txtTotalCost = r
    Else
        If Text50.Enabled = True Then
            r = b
            txtTotalCost = r
        ElseIf Text51.Enabled = True Then
            r = c
            txtTotalCost = r
        ElseIf Text52.Enabled = True Then
            r = d
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True Then
            r = a + b
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text51.Enabled = True Then
            r = a + c
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text52.Enabled = True Then
            r = a + d
            txtTotalCost = r
        ElseIf Text50.Enabled = True And Text51.Enabled = True Then
            r = b + c
            txtTotalCost = r
        ElseIf Text50.Enabled = True And Text52.Enabled = True Then
            r = b + d
            txtTotalCost = r
        ElseIf Text51.Enabled = True And Text52.Enabled = True Then
            r = c + d
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True And Text51.Enabled = True Then
            r = a + b + c
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True And Text52.Enabled = True Then
            r = a + b + d
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text51.Enabled = True And Text52.Enabled = True Then
            r = a + c + d
            txtTotalCost = r
        ElseIf Text50.Enabled = True And Text51.Enabled = True And Text52.Enabled = True Then
            r = b + c + d
            txtTotalCost = r
        ElseIf Text49.Enabled = True And Text50.Enabled = True And Text51.Enabled = True And Text52.Enabled = True Then
            r = a + b + c + d
            txtTotalCost = r
        End If
    End If

End Sub


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 09:23 AM

I have no idea what that means - especially in light of your textbox and long variable names.
Was This Post Helpful? 0
  • +
  • -

#7 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Re: Problem with adding in VB

Posted 05 September 2012 - 09:29 AM

The long was just for testing purposes to see is maybe int was not working. Need to change that back to INT. What I am trying to do in my program is select a rate. Depending on which rate is selected it should do the sum when you click total or (label45). For some reason it does not want to add up anything. I put a comment to each line to see where the error might be but no sum, only retrieves text49.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 09:35 AM

where's the addition in lines like this? I see assignment not addition.

12	        r = a
13	        txtTotalCost = r


Was This Post Helpful? 0
  • +
  • -

#9 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Re: Problem with adding in VB

Posted 05 September 2012 - 09:38 AM

View Postmodi123_1, on 05 September 2012 - 09:35 AM, said:

where's the addition in lines like this? I see assignment not addition.

12	        r = a
13	        txtTotalCost = r



If that option is selected, it will only display that rate.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 09:43 AM

You are missing the point - I am saying put a breakpoint on your code and see what is actually being executed. You'll find it is not what you think it is.
Was This Post Helpful? 0
  • +
  • -

#11 bebepolanco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-September 12

Re: Problem with adding in VB

Posted 05 September 2012 - 09:56 AM

The breakpoint is an awesome tool. Have lot to learn still. Many appreciations modi123_1. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Problem with adding in VB

Posted 05 September 2012 - 09:58 AM

No problem. Learn debugging now else you'll be in a rough place later!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1