If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > Just need a little help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-06-10, 02:55
ch4d ch4d is offline
Registered User
 
Join Date: Nov 2010
Posts: 10
Just need a little help

Code:
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ' Declaring variables
        Dim sintProductPrice As Single
        Dim sintQuantity As Single
        Dim sintTotal As Single

        ' Determining number and prices
        sintQuantity = txtQuantity.Text
      
        If cboProduct.Text = "AT94" Then sintProductPrice = 24.95 + (24.95 * 0.07)
        If cboProduct.Text = "BV06" Then sintProductPrice = 794.95 + (794.95 * 0.07)
        If cboProduct.Text = "CD52" Then sintProductPrice = 165.0 + (165.0 * 0.07)
        If cboProduct.Text = "DL71" Then sintProductPrice = 129.95 + (129.95 * 0.07)
        If cboProduct.Text = "DR93" Then sintProductPrice = 495.0 + (495.0 * 0.07)
        If cboProduct.Text = "DW11" Then sintProductPrice = 399.99 + (399.99 * 0.07)
        If cboProduct.Text = "FD21" Then sintProductPrice = 159.95 + (159.99 * 0.07)
        If cboProduct.Text = "KL62" Then sintProductPrice = 349.95 + (349.95 * 0.07)
        If cboProduct.Text = "KT03" Then sintProductPrice = 595.0 + (595.0 * 0.07)
        If cboProduct.Text = "KV29" Then sintProductPrice = 1390.0 + (1390.0 * 0.07)


        ' Order History
        sintTotal += sintProductPrice * sintQuantity
        lblTransaction.Text = "You added " & sintQuantity & " " & cboProduct.Text & "s" & _
         vbNewLine & lblTransaction.Text
        lblPrice.Text = FormatCurrency(sintTotal) & _
        vbNewLine & lblPrice.Text

When I click this button multiple times I want it to keep adding/accumulating the sum of the product but instead it keeps giving me the same number again and again. The value resets to zero somewhere. I thought the '+=' was all I had to do and I don't know where I am going wrong.
Reply With Quote
  #2 (permalink)  
Old 12-06-10, 04:12
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
I don't think += works in VBA
sintTotal = sintTotal + sintProductPrice * sintQuantity

mind you I don't like the look of that code, I don't like seeing magic numbers such as 0.07
it also looks like you are hard coding prices, and I guess sales tax.. never a smart idea. if for no other reason it requires a program change when the powers that be change things. personally I'd want to be shifting that (the price) into a product table and link to it. if the .07 is a sales tax, that like wise should be stored in a separate table, with a tax code linked to the product. that sway round you make the users make the changes, freeing up developer time. there is nothing worse than a user contacting the developer, usually at the last minute to demand changes at short notice. if you transfer the load to the users it becomes their problem not yours

just as a passing comment...
I think you can restructure your code
instead of
If cboProduct.Text = "KV29" Then sintProductPrice = 1390.0 + (1390.0 * 0.07)
try
If cboProduct.Text = "KV29" Then sintProductPrice = 1390.0 * 1.07

mind you could assign the value to sintProductPrice
and at the end of the block multiply by the 1.07
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 12-06-10, 13:43
ch4d ch4d is offline
Registered User
 
Join Date: Nov 2010
Posts: 10
Making progress



The 1.07 was a good idea. I replaced the parts of my code like you said but sadly it still isn't accumulating.
Reply With Quote
  #4 (permalink)  
Old 12-06-10, 13:49
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
so you need to delve through the code and work out why it isn't doing weaht you want it to do
my suspisicion is that you are only calling the code once, its not accumulating because you maybe resetting the invoice value each time you enter the code.

so you need to iterate through the list of selected items and find the value

that could be done by going through the listbox and pulling out the numbers after the $

if the products ordered was a class you could implement a method to do that.

I still think the idea of hardcoding a product price and sales tax is asking for trouble, even if it is an assignment
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 12-06-10, 18:28
ch4d ch4d is offline
Registered User
 
Join Date: Nov 2010
Posts: 10
Quote:
Originally Posted by healdem View Post
so you need to delve through the code and work out why it isn't doing weaht you want it to do
my suspisicion is that you are only calling the code once, its not accumulating because you maybe resetting the invoice value each time you enter the code.

so you need to iterate through the list of selected items and find the value

that could be done by going through the listbox and pulling out the numbers after the $

if the products ordered was a class you could implement a method to do that.

I still think the idea of hardcoding a product price and sales tax is asking for trouble, even if it is an assignment
Your lack of help disturbs me. You do the delving and I do the correcting.
Reply With Quote
  #6 (permalink)  
Old 12-20-10, 16:59
TopKick TopKick is offline
Registered User
 
Join Date: Dec 2010
Posts: 8
Oh dear. As healdem has already mentioned, hard-coding a product price and sales tax in code is just not the way to do it my friend. Can you imagine if you had 300 items in your ComboBox to select. You would need 300 If cboProduct.Text = statements to deal with it. Pricing should be held within a Products table or file (in my opinion).

The sintTotal variable needs to be declared Globaly (Public) or at least at the Form level?

The way the sintTotal variable is currently declared (at the procedure level) it will be cleared every time you select a item from the ComboBox and the btnAdd_Click event is finished. The sintTotal needs to be declared as a Public Variable at the top of your code (under the Option Explicit statement).

This very same variable can then be cleared when the Start Over button is selected by adding this to the Click event for that button:

sintTotal = 0

That should point you into the right direction.
Reply With Quote
  #7 (permalink)  
Old 12-21-10, 03:56
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
Quote:
Your lack of help disturbs me. You do the delving and I do the correcting.
ha, bleeding ha, thats not the way it works, we give you pointers on how you resolve your problem. in exceptional circumstances contributors may provide code, and may provide partially worked answers. however that rarely happens with assignments. in part thats becuase you need to do the steps to learn and demonstrate that you have learn't from the process. demanding answers short changes you and your education. if styudents are preapred to work at it most contributors will

help, especially if there is a thorny problem that you can't get round and shown committment to develop a soultion.

I'm guessing the assignment deadline has passed, however if it hasn't
you need to write a process / function which adds the cost of all items ordered
declaring a global variable isn't a smart idea.. its bad programming practise. granted there are some times when y'have to break the rules, but this isn't one of them


not knowing how you store you order items makes it difficult to know how to satrt

if you stroe items ordered in a table then its a simple recordset
if its in a combo box then you need to iterate through the combobox. you can store infromation in a combo box and not display.. Its nbot good practise to dispoaly codes such as AT94... it may make sense to the business, but it means stuff all to the customer. after all they want an iron, not an AT94.

so say you stuffed the code, description, price, sales tax and quatity ordered into a combo box
you could iterate through that combobox doing your calcualtion as required

heres a hint
open up your copy of Visual Studio
press the F1 key and read up on combo boxes, read up on the for each construct read up on the properties of a combo box
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #8 (permalink)  
Old 12-23-10, 01:36
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
However, the scope of the variable sIntTotal is at the event sub level, therefore, each time the event occurs, the variable is created anew, with a value of zero. You need some method of persisting this value between calls of the button click event.

You could create a form level variable. (which, for an intro to programming class is probably what's expected.)
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On