Results 1 to 6 of 6

Thread: VB Calculation code

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    2

    VB Calculation code

    Hello I am new to VB and am teaching myself. I am wanting to perform a simple calculation. Two text boxes (one for a price and the other for a quantity) which will subsequently display a total price in a third text box. Two activate the total I will press a button called calculate. Can anyone help with this as I have a tried a few codes and am getting a coding error. I am using a dim statement for my variables: Dim price, qty, total As Double (is double the correct data type to use?) Please help Thank you

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: VB Calculation code

    Show your code.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    2

    Re: VB Calculation code

    Code as below:

    Private Sub Command10_Click()
    Dim price, qty, total As Double
    Item_Price.Text = price
    Quantity.Text = qty
    Text11.Text = total
    Text11.Text = Format(price * qty, "0.0")

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: VB Calculation code

    Try this
    VB Code:
    1. Private Sub cmdcalculate_Click()
    2.     Dim price As Double
    3.     Dim qty As Integer
    4.     Dim total As Double
    5.     price = Val(Text1.Text)
    6.     qty = Val(Text2.Text)
    7.     Text3.Text = Format((price * qty), "000.00")
    8. End Sub

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: VB Calculation code

    Quote Originally Posted by mel1
    Dim price, qty, total As Double
    This Dims total as Double, but price and qty as Variants. You have to specify the type for each variable
    VB Code:
    1. Dim price As Double, qty As Double, total As Double
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: VB Calculation code

    VB Code:
    1. Private Sub Command10_Click()
    2. Dim price, qty, total As Double
    3.     [B]Item_Price.Text = price[/B]
    4.     [B]Quantity.Text = qty[/B]
    5.     Text11.Text = total
    6.     Text11.Text = Format(price * qty, "0.0")
    7. End Sub
    = moves the value from right to left.

    You are putting the values of the variables into the textboxes.
    For your calculation you need to do it the other way around.

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