Help with InputBox Function

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4959 Views - Last Post: 23 March 2012 - 10:12 AM Rate Topic: -----

#1 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Help with InputBox Function

Posted 22 March 2012 - 10:21 PM

I am coding a program that will allow the user to input 5 payrolls for Store 1, Store 2, and Store 3. The total of the 5 payrolls are then added together and shown in the respective Store's labels. A total label is also there to add up all the totals into one number. The numbers in the label must be in currency form.

My problem is the numbers don't add correctly when I hit calculate. The first two numbers add together, but then after that the number just seems to subtract a random amount.

I need to complete the program by tomorrow so I guess I don't have much time to waste and I need your guys' help!

What am I doing wrong??

So far I have this --

Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles storeOneInput.Click

        Dim payroll1input As String
        Dim payroll2input As String
        Dim payroll3input As String
        Dim pay As String
        Dim pay2 As String
        Dim pay3 As String

        ' get first payroll amounts
        For payroll1 = 1 To 5 Step 1

            payroll1input = InputBox("What is the payroll for store 1?", "Kenton Inc.", , , )

            Lblstore1.Text = Val(payroll1input) + Val(pay)
            pay = Val(payroll1input)

            Lblstore1.Text = ConvertToInteger(

        Next

        ' get second payroll amounts
        For payroll2 = 1 To 5 Step 1

            payroll2input = InputBox("What is the payroll for store 2?", "Kenton Inc.", , , )

            Lblstore2.Text = Val(payroll2input) + Val(pay2)
            pay2 = Val(payroll2input)


        Next

        ' get third payroll amounts
        For payroll3 = 1 To 5 Step 1

            payroll3input = InputBox("What is the payroll for store 3?", "Kenton Inc.", , , )

            Lblstore3.Text = Val(payroll3input) + Val(pay3)
            pay3 = Val(payroll3input)


        Next


        ' get the total amount of payrolls
        companyTotal.Text = pay + pay2 + pay3



    End Sub

    Private Sub storeOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lblstore1.Click

    End Sub

    Private Sub companyTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles companyTotal.Click

    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Help with InputBox Function

#2 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 22 March 2012 - 10:34 PM

Is ConvertToInteger a function? If so where is the code of that?
And why are you declaring as String? Much easier if you use Double as the type.

This post has been edited by nK0de: 22 March 2012 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 22 March 2012 - 10:40 PM

View PostnK0de, on 22 March 2012 - 10:34 PM, said:

Is ConvertToInteger a function? If so where is the code of that?
And why are you declaring as String? Much easier if you use Double as the type.


Okay, I changed to declaring as Doubles. That makes sense. I thought they had to be strings since they are user inputs.

And I accidentally forgot to delete the Convert to Integer function. I was trying to find a way to convert the numbers to a currency value with a dollar sign.

Can you please help me some more?

I would be so thankful!
Was This Post Helpful? 0
  • +
  • -

#4 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 22 March 2012 - 10:48 PM

well there are a few ways to convert a string to a currency value with the symbol.

Dim cure As Double 
cur = CDbl(TextBox1.Text)


FormatCurrency function is one.

Label1.Text = FormatCurrency(cur, 2)


This is another way.
Label1.Text = Format(cur, "c")


The above 2 methods show the output with the currency symbol defined in your system. Using the .ToString method is one too but it doesn't show the currency symbol. Only sets the format.
Label1.Text = cur.ToString("0.00")

This post has been edited by nK0de: 22 March 2012 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 22 March 2012 - 10:55 PM

So using that methodology, what would be the specific code in my example to change the numbers in the labels to currency?
Was This Post Helpful? 0
  • +
  • -

#6 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 22 March 2012 - 11:07 PM

Sorry, chief. That's not how it works. I ain't modifying your code. I've given you the examples. You try it out in your code, if you get stuck anywhere, post the problem back here. That's the way. c'mon, it's not hard. You just have to apply the function.
Was This Post Helpful? 0
  • +
  • -

#7 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 22 March 2012 - 11:13 PM

View PostnK0de, on 22 March 2012 - 11:07 PM, said:

Sorry, chief. That's not how it works. I ain't modifying your code. I've given you the examples. You try it out in your code, if you get stuck anywhere, post the problem back here. That's the way. c'mon, it's not hard. You just have to apply the function.


Sorry but when you try all week to program this dang thing and it keeps failing it gets frustrating. I am literally just a beginner so go easy on me. I have only been doing this for a couple of weeks. Just please continue to help me!

I adjusted the top of the code to this.

Dim payroll1input As Double

        Dim payroll2input As Double

        Dim payroll3input As Double

        Dim pay As Double
        Dim pay2 As Double
        Dim pay3 As Double

        Lblstore1.Text = FormatCurrency(payroll1input, 2)
        Lblstore2.Text = FormatCurrency(payroll1input, 2)
        Lblstore3.Text = FormatCurrency(payroll1input, 2)


It changed all the labels to the proper currency format.

Now on to my next problem.

The first two user inputs that are inputed are added correctly for each label, but then after that all hell breaks loose and it does some random calculation and I end up with weird numbers.

Is there something wrong with my original code that is making it do this?
Was This Post Helpful? 0
  • +
  • -

#8 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 22 March 2012 - 11:57 PM

Yes, programming can be really frustrating. And that frustration becomes worthwhile when you actually learn something at the end. Not to mention the pleasure of having a program that works all smooth.

Okay looking at your original code, I'd say this.

Remove the Val() and convert all the user input to Double. CDbl(<variable/textbox>)

And from looking at your code I can't see how you're getting weird numbers since I don't see the user input. You should Debug through your code and find where exactly it goes bonkers.
Was This Post Helpful? 0
  • +
  • -

#9 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 23 March 2012 - 12:13 AM

View PostnK0de, on 22 March 2012 - 11:57 PM, said:

Yes, programming can be really frustrating. And that frustration becomes worthwhile when you actually learn something at the end. Not to mention the pleasure of having a program that works all smooth.

Okay looking at your original code, I'd say this.

Remove the Val() and convert all the user input to Double. CDbl(<variable/textbox>)

And from looking at your code I can't see how you're getting weird numbers since I don't see the user input. You should Debug through your code and find where exactly it goes bonkers.


        Dim payroll1input As Double
        payroll1input = CDbl(Lblstore1.Text)

        Dim payroll2input As Double
        payroll2input = CDbl(Lblstore2.Text)

        Dim payroll3input As Double
        payroll3input = CDbl(Lblstore3.Text)

        Dim pay As Double
        Dim pay2 As Double
        Dim pay3 As Double

        Lblstore1.Text = FormatCurrency(payroll1input, 2)
        Lblstore2.Text = FormatCurrency(payroll2input, 2)
        Lblstore3.Text = FormatCurrency(payroll3input, 2)


Did I do it correctly?
Was This Post Helpful? 0
  • +
  • -

#10 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 23 March 2012 - 12:19 AM

Isn't Lblstore1 a label? You should convert the user input (payroll1input), not the output.
Was This Post Helpful? 0
  • +
  • -

#11 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 23 March 2012 - 12:26 AM

View PostnK0de, on 23 March 2012 - 12:19 AM, said:

Isn't Lblstore1 a label? You should convert the user input (payroll1input), not the output.


Gah. Thanks for catching that.

This is what I currently have now.

        Dim payroll1input As Double
        Lblstore1.Text = CDbl(payroll1input)

        Dim payroll2input As Double
        Lblstore2.Text = CDbl(payroll2input)

        Dim payroll3input As Double
        Lblstore3.Text = CDbl(payroll3input)

        Dim pay As Double
        Dim pay2 As Double
        Dim pay3 As Double

        Lblstore1.Text = FormatCurrency(payroll1input, 2)
        Lblstore2.Text = FormatCurrency(payroll2input, 2)
        Lblstore3.Text = FormatCurrency(payroll3input, 2)



The numbers still aren't adding correctly.
Was This Post Helpful? 0
  • +
  • -

#12 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 23 March 2012 - 12:31 AM

Re-write your loops with these new modifications and post the code.
Was This Post Helpful? 0
  • +
  • -

#13 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 23 March 2012 - 12:35 AM

This is what I have now. I don't know how I am supposed to delete the Val's without screwing up the entire program :S

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles storeOneInput.Click

        Dim payroll1input As Double
        Lblstore1.Text = CDbl(payroll1input)

        Dim payroll2input As Double
        Lblstore2.Text = CDbl(payroll2input)

        Dim payroll3input As Double
        Lblstore3.Text = CDbl(payroll3input)

        Dim pay As Double
        Dim pay2 As Double
        Dim pay3 As Double

        Lblstore1.Text = FormatCurrency(payroll1input, 2)
        Lblstore2.Text = FormatCurrency(payroll2input, 2)
        Lblstore3.Text = FormatCurrency(payroll3input, 2)



        ' get first payroll amounts

        For payroll1 = 1 To 5 Step 1

            payroll1input = InputBox("What is the payroll for store 1?", "Kenton Inc.", , , )

            Lblstore1.Text = Val(payroll1input) + Val(pay)
            pay = Val(payroll1input)

            Lblstore1.Text = FormatCurrency(payroll1input, 2)



        Next

        ' get second payroll amounts
        For payroll2 = 1 To 5 Step 1

            payroll2input = InputBox("What is the payroll for store 2?", "Kenton Inc.", , , )

            Lblstore2.Text = Val(payroll2input) + Val(pay2)
            pay2 = Val(payroll2input)


        Next

        ' get third payroll amounts
        For payroll3 = 1 To 5 Step 1

            payroll3input = InputBox("What is the payroll for store 3?", "Kenton Inc.", , , )

            Lblstore3.Text = Val(payroll3input) + Val(pay3)
            pay3 = Val(payroll3input)


        Next


        ' get the total amount of payrolls
        companyTotal.Text = Lblstore1.Text + Lblstore2.Text + Lblstore3.Text





    End Sub

Was This Post Helpful? 0
  • +
  • -

#14 tivoli   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-March 12

Re: Help with InputBox Function

Posted 23 March 2012 - 12:51 AM

I also just realized that I need an two counters that will help me do this. One for the outside loop and one for the inside loop.

This pseudocode is what I need to carry out.

Do while numberStore <=3

' calculate each store's total
Do while numPayroll<=5
calculate storePayroll
increase numPayroll by 1...
Loop

'update the numberStore increased by 1

'display the store's total storePayroll
Loop

I have trouble with loops and counters. I am not sure how I would put those in my code.
Was This Post Helpful? 0
  • +
  • -

#15 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Help with InputBox Function

Posted 23 March 2012 - 01:10 AM

View Posttivoli, on 23 March 2012 - 01:05 PM, said:

This is what I have now. I don't know how I am supposed to delete the Val's without screwing up the entire program :S

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles storeOneInput.Click

        Dim payroll1input As Double
        Lblstore1.Text = CDbl(payroll1input)

        Dim payroll2input As Double
        Lblstore2.Text = CDbl(payroll2input)

        Dim payroll3input As Double
        Lblstore3.Text = CDbl(payroll3input)

        Dim pay As Double
        Dim pay2 As Double
        Dim pay3 As Double

        Lblstore1.Text = FormatCurrency(payroll1input, 2)
        Lblstore2.Text = FormatCurrency(payroll2input, 2)
        Lblstore3.Text = FormatCurrency(payroll3input, 2)



        ' get first payroll amounts

        For payroll1 = 1 To 5 Step 1

            payroll1input = InputBox("What is the payroll for store 1?", "Kenton Inc.", , , )

            Lblstore1.Text = Val(payroll1input) + Val(pay)
            pay = Val(payroll1input)

            Lblstore1.Text = FormatCurrency(payroll1input, 2)

        Next



Okay, I made a small mistake. I only saw it when I re-created your situation.
Declare the user input from the inputbox as String first because the it returns the value as a string. Then convert it to Double later.

        Dim payroll1input As String
        Dim pay As Double

        For payroll1 = 1 To 5

            payroll1input = InputBox("What is the payroll for store 1?", "title")
            payroll1input = CDbl(payroll1input) + CDbl(pay)
            Label1.Text = FormatCurrency(payroll1input, 2)

        Next


And I have a question, from where are you assigning a value to the variable pay?

This post has been edited by nK0de: 23 March 2012 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2