cdk71
asked on
How do I find a calculation for a discount using a check box? (beginner)
I'm taking an intro to Visual Basic class in college and the professor isn't using the book because it's too complicated (why we had to buy it I don't know), however he does visuals in class. In this project he never showed us when we have if then statements with option buttons marked along with having a checkbox checked how to do a calculation.
My if then statements aren't working for the discount calculations.
I get the amount in the lbl but not with the discount or the new amount. What am I doing wrong? Please be patient with me I'm a beginner :( :( :(
These are the codes I"m working with:
The calculate code should find the movie price, calculate a 10% discount for members when the checkbox is checked and also give the amounts of the movies when they are a new release if the check box for new release is checked.
Thanks in advance and I'm sorry this is such a stupid question but I'm not sure what i'm paying good money for if he's not teaching this in class. :(
========================== ========== ==
'Constants for calculations
Const mcurDiscountRate As Currency = 0.1
Const mcurRentalCostDvd As Currency = 2.5
Const mcurRentalCostVHS As Currency = 1.8
Const mcurRentalCostnewreleaseDV D As Currency = 3
Const mcurRentalcostnewreleaseVH S As Currency = 2
Dim mcurTotalRentalAmounts As Currency
Dim curTotalCustomerSummary As Integer
Dim curDiscount As Currency
Option Explicit
Private Sub cmdCalculate_Click()
'Set Variables for Calculations
Dim mcurtotal As Currency
Dim curamountdue As Currency
Dim mintMovieRentals As Integer
Dim mintSummaryofCustomers As Integer
'Perform Data Validation
If txtMovieTitle.Text = "" Then
MsgBox "Please enter the movie title", vbOKOnly, "Data Entry Error"
txtMovieTitle.SetFocus
Exit Sub
End If
'Find the amounts for the movies rented for nonmembers and not a new release
If optDVD.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalCostnewreleaseDV D
ElseIf optVHS.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalcostnewreleaseVH S
ElseIf optDVD.Value = True Then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = True Then
mcurtotal = mcurRentalCostVHS
End If
'Find Discount if the member box is checked for a member with a 10% discount <--- this is the part that's not working
If chkMember.Value = True Then
curDiscount = mcurtotal * mcurDiscountRate
mcurtotal = mcurtotal - curDiscount <---- the professor said this was correct and the discount isn't showing up
End If in the discount lbl nor is the discount showing up in the lbltotalsale. The original amount is still showing up and the discount is $0.00
'Format and display answers for calculating rentals
lblTotalSale.Caption = FormatCurrency(mcurtotal)
lbldiscount.Caption = FormatCurrency(curDiscount )
My if then statements aren't working for the discount calculations.
I get the amount in the lbl but not with the discount or the new amount. What am I doing wrong? Please be patient with me I'm a beginner :( :( :(
These are the codes I"m working with:
The calculate code should find the movie price, calculate a 10% discount for members when the checkbox is checked and also give the amounts of the movies when they are a new release if the check box for new release is checked.
Thanks in advance and I'm sorry this is such a stupid question but I'm not sure what i'm paying good money for if he's not teaching this in class. :(
==========================
'Constants for calculations
Const mcurDiscountRate As Currency = 0.1
Const mcurRentalCostDvd As Currency = 2.5
Const mcurRentalCostVHS As Currency = 1.8
Const mcurRentalCostnewreleaseDV
Const mcurRentalcostnewreleaseVH
Dim mcurTotalRentalAmounts As Currency
Dim curTotalCustomerSummary As Integer
Dim curDiscount As Currency
Option Explicit
Private Sub cmdCalculate_Click()
'Set Variables for Calculations
Dim mcurtotal As Currency
Dim curamountdue As Currency
Dim mintMovieRentals As Integer
Dim mintSummaryofCustomers As Integer
'Perform Data Validation
If txtMovieTitle.Text = "" Then
MsgBox "Please enter the movie title", vbOKOnly, "Data Entry Error"
txtMovieTitle.SetFocus
Exit Sub
End If
'Find the amounts for the movies rented for nonmembers and not a new release
If optDVD.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalCostnewreleaseDV
ElseIf optVHS.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalcostnewreleaseVH
ElseIf optDVD.Value = True Then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = True Then
mcurtotal = mcurRentalCostVHS
End If
'Find Discount if the member box is checked for a member with a 10% discount <--- this is the part that's not working
If chkMember.Value = True Then
curDiscount = mcurtotal * mcurDiscountRate
mcurtotal = mcurtotal - curDiscount <---- the professor said this was correct and the discount isn't showing up
End If in the discount lbl nor is the discount showing up in the lbltotalsale. The original amount is still showing up and the discount is $0.00
'Format and display answers for calculating rentals
lblTotalSale.Caption = FormatCurrency(mcurtotal)
lbldiscount.Caption = FormatCurrency(curDiscount
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for check box possible values are checked/unchecked/gray and you were checking "If chkMember.Value = True Then" always becomes false, so never going inside the true part.
This is NOT FOR THE POINTS, jues edicational value. Since you're learning, I should point out that VB interprets ALL If statements that contain AND and OR conditions. This is inefficient. So your example should be re-written:
Your example:
If optDVD.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalCostnewreleaseDV D
ElseIf optVHS.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalcostnewreleaseVH S
ElseIf optDVD.Value = True Then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = True Then
mcurtotal = mcurRentalCostVHS
End If
So, if this is a VHS rental (non-new release), VB needs to go through 6 checks (optDVD.Value = True , chkNewRelease.Value = Checked, optVHS.Value = True , chkNewRelease.Value = Checked again, optDVD.Value = True again, and then finally optVHS.Value = True again)
Should become something like:
if chkNewRelease.Value = Checked then
if optDVD.Value = true then
mcurtotal = mcurRentalCostnewreleaseDV D
elseif optVHS.Value = True then
mcurtotal = mcurRentalcostnewreleaseVH S
endif
else
If optDVD.Value = true then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = true then
mcurtotal = mcurRentalCostVHS
End if
endif
So, if this is a VHS rental (non-new release), VB needs to go through 2 checks ONLY (chkNewRelease.Value = Checked and optVHS.Value = True).
Enjoy VB programming!
Your example:
If optDVD.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalCostnewreleaseDV
ElseIf optVHS.Value = True And chkNewRelease.Value = Checked Then
mcurtotal = mcurRentalcostnewreleaseVH
ElseIf optDVD.Value = True Then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = True Then
mcurtotal = mcurRentalCostVHS
End If
So, if this is a VHS rental (non-new release), VB needs to go through 6 checks (optDVD.Value = True , chkNewRelease.Value = Checked, optVHS.Value = True , chkNewRelease.Value = Checked again, optDVD.Value = True again, and then finally optVHS.Value = True again)
Should become something like:
if chkNewRelease.Value = Checked then
if optDVD.Value = true then
mcurtotal = mcurRentalCostnewreleaseDV
elseif optVHS.Value = True then
mcurtotal = mcurRentalcostnewreleaseVH
endif
else
If optDVD.Value = true then
mcurtotal = mcurRentalCostDvd
ElseIf optVHS.Value = true then
mcurtotal = mcurRentalCostVHS
End if
endif
So, if this is a VHS rental (non-new release), VB needs to go through 2 checks ONLY (chkNewRelease.Value = Checked and optVHS.Value = True).
Enjoy VB programming!
ASKER
Thank you so much! It worked! :)
I really do appreciate this! It's amazing how one word is the key to the programming (which this word wasn't found in my text book or my notes).
Does anyone have any recommendations for a textbook for programming as a beginner for VB?
Right now they had us buy Programming in Visual Basic 6.0 by Julia Case Bradley and Anita C. Millspaugh. I'm finding the examples completely confusing. :(
Chris.
I really do appreciate this! It's amazing how one word is the key to the programming (which this word wasn't found in my text book or my notes).
Does anyone have any recommendations for a textbook for programming as a beginner for VB?
Right now they had us buy Programming in Visual Basic 6.0 by Julia Case Bradley and Anita C. Millspaugh. I'm finding the examples completely confusing. :(
Chris.