how to do calculations with multiple check boxes in vb.net? - vb.net

for example i have 5 check boxes and these check boxes are names of products with different prices which are constant and displayed in five text boxes when a certain check box is checked
and i also have 5 text boxes for the quantity which depends on the input of the user. now for example i only checked 2 check boxes and input their quantity let say product one = 100 and product 2 = to 200 and i input 2 for both of their quantity display the total amount in only one text box. how can i code it because when i use the if else statement, it only calculate and display the price of one product while i have chosen or checked 2 products and when i used or in the if statement, when i only checked 2 check boxes and input their quantities, i get an error which is format string unhandle. This is my code:
If chkPopcorn.Checked = True Then
quantity1 = Integer.Parse(txtQuantityPopcorn.Text)
price1 = txtPricePopcorn.Text * quantity1
ElseIf chkBurger.Checked = True Then
quantity2 = Integer.Parse(txtQuantityBurger.Text)
price2 = txtPriceBurger.Text * quantity2
ElseIf chkSpaghetti.Checked = True Then
quantity3 = Integer.Parse(txtQuantitySpaghetti.Text)
price3 = txtPriceSpaghetti.Text * quantity3
ElseIf chkHotdog.Checked = True Then
quantity4 = Integer.Parse(txtQuantityHotdog.Text)
price4 = txtPriceHotdog.Text * quantity4
ElseIf chkCupcake.Checked = True Then
quantity5 = Integer.Parse(txtQuantityCupcake.Text)
price5 = txtPriceCupcake.Text * quantity5
End If
End If
txtSales.Text = price1 + price2 + price3 + price4 + price5

Simple to fix, use 5 If statements in a row, instead of ElseIf
If chkPopcorn.Checked = True Then
quantity1 = Integer.Parse(txtQuantityPopcorn.Text)
price1 = txtPricePopcorn.Text * quantity1
End If
If chkBurger.Checked = True Then
quantity2 = Integer.Parse(txtQuantityBurger.Text)
price2 = txtPriceBurger.Text * quantity2
End If
If chkSpaghetti.Checked = True Then
quantity3 = Integer.Parse(txtQuantitySpaghetti.Text)
price3 = txtPriceSpaghetti.Text * quantity3
End If
If chkHotdog.Checked = True Then
quantity4 = Integer.Parse(txtQuantityHotdog.Text)
price4 = txtPriceHotdog.Text * quantity4
End If
If chkCupcake.Checked = True Then
quantity5 = Integer.Parse(txtQuantityCupcake.Text)
price5 = txtPriceCupcake.Text * quantity5
End If
txtSales.Text = price1 + price2 + price3 + price4 + price5
End Sub

Related

Is there any possible way to show all the material name in datagridview, even though the other column is empty?

In my inventory program there was 3 tables in my database.
1. Input
Column(id,Material,Quantity)
2. Output
Column(id,Material,Quantity)
3. MaterialList (Table) Where I can input the names of materials
Column(id,Material)
In my remaining Stocks form I want to show the Remaining Stocks of all MaterialList including the material that has no stocks.
For example:
From MaterialList From Input and Output Quantity
Material list Remaining Stocks
item1 150
item2
item3
item4 200
I tried this but there's no hope.
conn.Open()
conn = New MySqlConnection
conn.ConnectionString = "server=localhost;userid=root;password=SOUTHEAST;database=reportingsystem"
Dim searchquery As String = "select reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials, reportingsystem.rawmaterialswarehouseandrawmaterials.safetystocks, (Sum(reportingsystem.rawmaterialsinput.Quantity) - sum(reportingsystem.rawmaterialsoutput.Quantity)) as 'Remaining Stocks' from reportingsystem.rawmaterialsinput, reportingsystem.rawmaterialsoutput, reportingsystem.rawmaterialswarehouseandrawmaterials where reportingsystem.rawmaterialsinput.RawMaterial = reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials" ' JOIN reportingsystem.rawmaterialsinput.RawMaterial ON reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials = reportingsystem.rawmaterialsinput.RawMaterial ORDER BY reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials"
'Select rawmaterialsinput.DeliveryDate as 'Delivery Date', rawmaterialswarehouseandrawmaterials.Rawmaterials, (sum(rawmaterialsinput.Quantity) - sum(rawmaterialsoutput.Quantity)) as 'Quantity' , rawmaterialswarehouseandrawmaterials.safetystocks from reportingsystem.rawmaterialsinput, reportingsystem.rawmaterialsoutput, reportingsystem.rawmaterialswarehouseandrawmaterials where rawmaterialsinput.RawMaterial = rawmaterialsoutput.RawMaterial
'"Select rawmaterialswarehouseandrawmaterials.Rawmaterials as 'Raw Materials', rawmaterialswarehouseandrawmaterials.safetystocks as 'Safety Stock', (sum(rawmaterialsinput.Quantity) - sum(rawmaterialsoutput.Quantity)) as 'Remaining Stocks' where rawmaterialsinput.RawMaterial = rawmaterialswarehouseandrawmaterials.Rawmaterials FROM reportingsystem.rawmaterialswarehouseandrawmaterials, reportingsystem.rawmaterialsinput, reportingsystem.rawmaterialsoutput"
'Select rawmaterialswarehouseandmaterials.Rawmaterials, (sum(rawmaterialsinput.Quantity) - sum(rawmaterialsoutput.Quantity)) as 'Remaining Stocks', rawmaterialswarehouseandrawmaterials.safetystocks FROM reportingsystem.rawmaterialswarehouseandrawmaterials, reportingsystem.rawmaterialsinput, reportingsystem.rawmaterialsoutput JOIN input ON rawmaterialsinput.RawMaterial = rawmaterialswarehouseandrawmaterials.Rawmaterials
Dim commander As New MySqlCommand(searchquery, conn)
Dim adapter As New MySqlDataAdapter(commander)
monitoringdata.Clear()
adapter.Fill(monitoringdata)
MonitoringDGV.DataSource = monitoringdata
For Each row As DataGridViewRow In MonitoringDGV.Rows
Dim quantity As Integer = Val(row.Cells(1).Value)
Dim safetystocks As Integer = Val(row.Cells(2).Value)
If safetystocks < quantity Then
row.DefaultCellStyle.BackColor = Color.Red
ElseIf safetystocks < (quantity * 1.2) Then
row.DefaultCellStyle.BackColor = Color.Green
ElseIf safetystocks > (quantity * 1.2) Then
row.DefaultCellStyle.BackColor = Color.Green
End If
Next
conn.Close()
What I really need is show all of the Material Name in column 1 and show the remaining stocks in column 2.
Example:
Material Remaining Stocks
Item1 1
Item2 4
Item3
Item4
Item5 18
Item6 30
Item7

display the total based on the inputted amount in the datagridview

i have a textbox where i can input any amount for example 3,000.00, then a datagridview that looks like
Category | Amount
Supmat | 2,000
POL | 500
Others | 400
POL | 500
to get the total i used this code
For i As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Rows(i).Cells("Category").Value = "Supplies/Materials" Then
supmat += DataGridView1.Rows(i).Cells("Amount").Value
ElseIf DataGridView1.Rows(i).Cells("Category").Value = "Legal Expenses" Then
legal += DataGridView1.Rows(i).Cells("Amount").Value
ElseIf DataGridView1.Rows(i).Cells("Category").Value = "POL" Then
pol += DataGridView1.Rows(i).Cells("Amount").Value
ElseIf DataGridView1.Rows(i).Cells("Category").Value = "Others" Then
oth += DataGridView1.Rows(i).Cells("Amount").Value
End If
If CDec(txtFund.Text) > supmat Then
supmat1 = supmat
remain = CDec(txtFund.Text) - supmat
If legal > 0 Then
If remain > legal Then
legal1 = legal
remain2 = remain - legal
If pol > 0 Then
If remain2 > pol Then
pol1 = pol
remain3 = remain2 - pol
If oth > 0 Then
If remain3 > oth Then
oth1 = oth
Else
oth1 = remain3
End If
Else
oth1 = remain3
End If
Else
pol1 = remain2
oth1 = 0
End If
ElseIf oth > 0 Then
If remain2 > oth Then
oth1 = oth
Else
oth1 = remain2
End If
End If
Else
legal1 = remain
pol1 = 0
oth1 = 0
End If
ElseIf pol > 0 Then
If remain > pol Then
pol1 = pol
remain2 = remain - pol
If oth > 0 Then
If remain2 > oth Then
oth1 = oth
Else
oth1 = remain2
End If
Else
oth1 = remain2
End If
Else
pol1 = remain
oth1 = 0
End If
ElseIf oth > 0 Then
If remain > oth Then
oth1 = oth
Else
oth1 = remain
End If
End If
Else
supmat1 = CDec(txtFund.Text)
legal1 = 0
pol1 = 0
oth1 = 0
End If
Next
i got the total of 3000 where the output is
Supmat = 2000
Legal = 0
POL = 1000
Others = 0
but the output i need is
Supmat = 2000
Legal = 0
POL = 600
Others = 400
if you check my category i inputted the Supmat 2000 first then POL 500 then Others 400 and last is POL 500 again. i only need to get the total of 3000 but since the last POL is 500 i should only get the 100 from it. since i already have the 2000 + 500 + 400 from the first three.
can someone help with the code where whatever i inputted in my datagridview that should be displayed first instead of getting the total of every category.
sample picture
I attached a sample picture on how i want my output is.
the code above can do the output
Supmat = 2000
Legal = 0
POL = 1000
Others = 0
Try this... Loop thru your data passing the total value and each subtotal value to calcTotal which handles the actual calculations and updates the totals. When the total value <= 0, exit the loop.
Private Sub MainCalculation()
Dim supmat As Decimal = 0
Dim legal As Decimal = 0
Dim pol As Decimal = 0
Dim oth As Decimal = 0
Dim total_value As Decimal = CDec(Me.txtFund.Text)
For i As Integer = 0 To DataGridView1.RowCount - 1
Dim t As Decimal = DataGridView1.Rows(i).Cells("Amount").Value
Select Case DataGridView1.Rows(i).Cells("Category").Value
Case "Supplies/Materials"
calcTotal(supmat, total_value, t)
Case "Legal Expenses"
calcTotal(legal, total_value, t)
Case "POL"
calcTotal(pol, total_value, t)
Case "Others"
calcTotal(oth, total_value, t)
End Select
If total_value <= 0 Then Exit For
Next
Debug.Print("Supmat = " & supmat)
Debug.Print("Legal = " & legal)
Debug.Print("POL = " & pol)
Debug.Print("Others = " & oth)
End Sub
Private Sub calcTotal(ByRef subtotal As Decimal, ByRef total_value As Decimal, t As Decimal)
If total_value > t Then
subtotal += t
total_value -= t
Else
subtotal += total_value
total_value = 0
End If
End Sub

Why is my Select Case not working

Okay my question goes as follows; I think i coded everything right execpt for the part where i do my select case, I want the first 3 Flavours to only cost 55 cents but when I do my code it always makes the scoops 65 cents no matter what icecream type i select and i dont know how to make it change, i thought i had it right but it isnt working
Public Class frmJoeyIceCreamParlour
Const MIN_SCOOPS = 1
Const MAX_SCOOPS = 9
Const BASIC_FLAVOUR = 0.55
Const PREMIUM_FLAVOUR = 0.65
Const TOPPING = 0.6
Const DEEZ_NUTS = 0.5
Const WHIPPED_CREAM = 0.65
Public scoopEntry As Single
Public scoopType As Double
Public runningTotal As Double
Private Sub frmJoeyIceCreamParlour_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstFlavours.Items.Add("Vanilla")
lstFlavours.Items.Add("Chocolate")
lstFlavours.Items.Add("Strawberry")
lstFlavours.Items.Add("Mango")
lstFlavours.Items.Add("Bananna")
lstFlavours.Items.Add("Grape")
lstFlavours.Items.Add("Mint Chocolate Chip")
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If txtScoops.Text = Nothing Then
MessageBox.Show("Please enter a value in the scoops category.")
txtScoops.Focus()
ElseIf Not IsNumeric(txtScoops.Text) Then
MessageBox.Show("Entry must be numeric! Please try again.")
txtScoops.Focus()
ElseIf txtScoops.Text < MIN_SCOOPS Or txtScoops.Text > MAX_SCOOPS Then
MessageBox.Show("Please enter a number between 1 and 9 scoops.")
txtScoops.Focus()
ElseIf lstFlavours.SelectedItem = Nothing Then
MessageBox.Show("Please select a flavour.")
ElseIf rdoNoTopping.Checked = False And rdoOneTopping.Checked = False And rdoTwoTopping.Checked = False And rdoThreeTopping.Checked = False Then
MessageBox.Show("Please select the amount of toppings you would like.")
Else
Dim number As Integer = 7
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
runningTotal = scoopType * Double.Parse(txtScoops.Text)
End Select
If rdoOneTopping.Checked = True Then
runningTotal = runningTotal + TOPPING
ElseIf rdoTwoTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 2)
ElseIf rdoThreeTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 3)
End If
If chkWhippedCream.Checked = True Then
runningTotal = runningTotal + WHIPPED_CREAM
End If
If chkNuts.Checked = True Then
runningTotal = runningTotal + DEEZ_NUTS
End If
lblOutputTotal.Text = (FormatCurrency(runningTotal))
End If
End Sub
End Class
You have it hard coded to use 7 via the line just above your Select case number statement: Dim number As Integer = 7. You should instead be looking up the selected index by doing something like Dim number As Integer = lstFlavours.SelectedIndex
Dim number As Integer = lstFlavours.SelectedIndex
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
End Select
runningTotal = scoopType * Double.Parse(txtScoops.Text)

sql routine to export separate txt files

I have an sql query which is a pricelist that I normally run 18 different times for 18 different clients.
I have a #varchar as as declaration and I change the customerID. This then exports a txt flat-file file so then I can convert to Excel and send via feebootimail.
Is there a routine that I can run in sql 2005 so I can feed a list of customers into and get 18 different txt files? example Pricelist_8343.txt, Pricelist_8363.txt ect
DECLARE #CustNum varchar(20)
SELECT #CustNum = '7509'
SELECT
it.ITEMID 'item id'
,it.ItemGroupID
,it.ITEMNAME
,Convert(Decimal(9,0),itm.QUANTITY) 'Sales Multiple'
,Convert(Decimal(9,0),it.TAXPACKAGINGQTY) 'Price Break Qty'
,Convert(Decimal(9,0),it.OUTERQUANTITY) 'Carton Qty'
,Convert(Decimal(9,2),itm.PRICE) 'Part Pack'
,Convert(Decimal(9,2),itm.PRICE2) 'Full Pack'
,Convert(Decimal(9,2),round( CASE
WHEN pdt.AMOUNT >0 then pdt.amount
else CASE
when pdt.discpct is null then CASE
WHEN pdt.PRICELEVELTOUSE = 0 then Price
WHEN pdt.PRICELEVELTOUSE = 1 then Price2
WHEN pdt.PRICELEVELTOUSE = 2 then Price3
WHEN pdt.PRICELEVELTOUSE = 3 then Price4
WHEN pdt.PRICELEVELTOUSE = 4 then Price5
WHEN pdt.PRICELEVELTOUSE = 5 then Price6 END
when pdt.discpct = 0 then CASE
WHEN pdt.PRICELEVELTOUSE = 0 then Price
WHEN pdt.PRICELEVELTOUSE = 1 then Price2
WHEN pdt.PRICELEVELTOUSE = 2 then Price3
WHEN pdt.PRICELEVELTOUSE = 3 then Price4
WHEN pdt.PRICELEVELTOUSE = 4 then Price5
WHEN pdt.PRICELEVELTOUSE = 5 then Price6 END
when pdt.discpct > 0 then CASE
WHEN pdt.PRICELEVELTOUSE = 0 then round(itm.price - (itm.price*pdt.discpct/100),2)
WHEN pdt.PRICELEVELTOUSE = 1 then round(itm.price2 - (itm.price2*pdt.discpct/100),2)
WHEN pdt.PRICELEVELTOUSE = 2 then round(itm.price3 - (itm.price3*pdt.discpct/100),2)
WHEN pdt.PRICELEVELTOUSE = 3 then round(itm.price4 - (itm.price4*pdt.discpct/100),2)
WHEN pdt.PRICELEVELTOUSE = 4 then round(itm.price5 - (itm.price5*pdt.discpct/100),2)
WHEN pdt.PRICELEVELTOUSE = 5 then round(itm.price6 - (itm.price6*pdt.discpct/100),2) END END END ,2)) as 'TAPrice'
,upper(itm.unitid) 'UOM'
, Case
When itm.PRICECHANGESTATUS=3 then 'Increase'
When itm.PRICECHANGESTATUS=2 then 'Decrease'
When itm.PRICECHANGESTATUS=1 then 'New Item'
When itm.PRICECHANGESTATUS=0 then '-'
END 'Price Indicator'
from INVENTTABLE it
join INVENTTABLEMODULE itm on it.ItemId = itm.ItemID and itm.ModuleType = 2
join CUSTTABLE cust on LTRIM(cust.AccountNum) = #CustNum
left outer join PRICEDISCTABLE pdt on (ltrim(pdt.accountrelation) =
case
when pdt.accountcode = 0 then ltrim(cust.accountnum)
when pdt.accountcode = 1 then ltrim(cust.pricegroup) end)and
(ltrim(pdt.ItemRelation)+ltrim(pdt.UnitID) = case
when pdt.itemrelation = it.itemid then ltrim(it.ItemID)+ltrim(itm.Unitid)
when pdt.itemrelation = it.itempricegroup then ltrim(it.ItemPriceGroup)+ltrim(itm.Unitid)end
) and pdt.fromdate <= getdate() and (getdate()<= pdt.todate or pdt.todate = ' ')
join PriceLevelListReportLine sorter on it.ItemGroupID = sorter.ItemGroupID
and (it.ItemType = (case when sorter.linetype = 0 then '0'
when sorter.linetype = 1 then '0'
when sorter.linetype = 2 then '1'end)
or it.ItemType = (case when sorter.linetype = 1 then '1' end))
WHERE it.PRICELISTFlag=1
ORDER BY sorter.SortOrder, it.ItemID
You could do this with SQLCMD and a batch file.
Make a script.sql file with contents like:
declare #CustNum varchar(20) = '$(custnum)'
select
-- your giant query here
Then make a batch file that looks like this:
#echo off
set custnum=1234
call :export
set custnum=5678
call :export
set custnum=9012
call :export
goto :eof
:export
sqlcmd -E -i script.sql -o %custnum%.txt -v CustNum=%custnum%
goto :eof
Copy and paste the set/call lines once for each of your 18 customers.
Instead of copy+paste, you could do something with a for loop if you have a list of the customers somewhere - output of another SQLCMD query, or just a text file. I leave that to you to figure out.

VBA UDF returning #VALUE!

So I have a pretty simple UDF written in visual basic for use in excel. It calculates your approx. taxes. Lets say I use it as such:
=Taxes(I23-I18,I24-I20,"Married")
If I type this in it works great. Now if I save the sheet and restart excel the cell now says #VALUE! If I select the formula and hit enter once again it recalculates it fine. What am I doing wrong? Application.Volatile shouldn't be needed but I was trying ideas...
Private Type TaxBracket
Perc As Double
Floor As Currency
Limit As Currency
End Type
Public Function Taxes(gross1 As Currency, gross2 As Currency, filingStatus As String) As Currency
Application.Volatile True
Dim brackets(6) As TaxBracket
Dim stdDeduction As Currency
Dim ssTaxRate As Double
Dim medicareTaxRate As Double
Dim Tax As Double
stdDeduction = 5700
ssTaxRoof = 106800
ssTaxRate = 0.062
medicareTaxRate = 0.0145
Tax = medicareTaxRate * (gross1 + gross2)
Tax = Tax + IIf(gross1 < ssTaxRoof, ssTaxRate * gross1, ssTaxRate * ssTaxRoof)
Tax = Tax + IIf(gross2 < ssTaxRoof, ssTaxRate * gross2, ssTaxRate * ssTaxRoof)
brackets(0).Perc = 0.1
brackets(1).Perc = 0.15
brackets(2).Perc = 0.25
brackets(3).Perc = 0.28
brackets(4).Perc = 0.33
brackets(5).Perc = 0.35
If filingStatus = "Single" Then
brackets(0).Floor = 0
brackets(1).Floor = 8375
brackets(2).Floor = 34000
brackets(3).Floor = 82400
brackets(4).Floor = 171850
brackets(5).Floor = 373650
brackets(0).Limit = 8375
brackets(1).Limit = 34000
brackets(2).Limit = 82400
brackets(3).Limit = 171850
brackets(4).Limit = 373650
brackets(5).Limit = 1000000000
Tax = Tax + incomeTaxes(gross1, brackets, stdDeduction) + incomeTaxes(gross2, brackets, stdDeduction)
ElseIf filingStatus = "Married" Then
brackets(0).Floor = 0
brackets(1).Floor = 16750
brackets(2).Floor = 68000
brackets(3).Floor = 137300
brackets(4).Floor = 209250
brackets(5).Floor = 373650
brackets(0).Limit = 16750
brackets(1).Limit = 68000
brackets(2).Limit = 137300
brackets(3).Limit = 209250
brackets(4).Limit = 373650
brackets(5).Limit = 1000000000
Tax = Tax + incomeTaxes(gross1 + gross2, brackets, stdDeduction * 2)
Else
Taxes = "N/A"
Return
End If
Taxes = Tax
End Function
Private Function incomeTaxes(gross As Currency, brackets() As TaxBracket, deduction As Currency) As Currency
Dim Tax As Double
Dim taxable As Double
Tax = 0
taxable = gross - deduction
For i = 0 To 5
If taxable > brackets(i).Limit Then
Tax = Tax + (WorksheetFunction.Min(taxable, brackets(i).Limit) - brackets(i).Floor) * brackets(i).Perc
Else
If taxable > brackets(i).Floor Then
Tax = Tax + (taxable - brackets(i).Floor) * brackets(i).Perc
Else
'tax = tax
End If
End If
Next i
incomeTaxes = Tax
End Function
Your UDF's look OK, apart from using Currency data types (probably should be using doubles or variants since that is what Excel uses).
The usual reason for getting #Value with a UDF is that one of the input arguments cannot be converted to the correct type. If your input cells do not contain numeric values when the workbook opens you would get #Value. This might be caused by calculation sequence problems resulting in one of the upstream precedent cells being uncalculated the first time the function is called. Try declaring the input parameters as variant rather than currency and add some temporary debug.print statements to show the input parameters in the Immediate window.