I have 2 items in a ComboBox which are "Eating In" and "Eating Out".
I want the program to add 20% VAT if the user selects "Eating In". If the user selects "Eating Out" I don't want any VAT adding.
I have the following code to work out my total which has worked successfully. I just need to find out how to assign the item in the ComboBox to add VAT.
dTotalCost = dTotalSandwichCost + dTotalDrinkCost + dTotalExtraCost
A possibility is to declare an Enum and calculate based on it. e.g.,
(Psuedocode, untested, just to give you an idea)
Enum AvailableEatingOptions
EatingIn
EatingOut
End Enum
then write like:
If comboBox.selectedIndex = AvailableEatingOptions.EatingIn Then
dVAT = GivenVATValue * 0.2 ' Take 20% of your reference value
Else
dVAT = 0
End If
then add this dVAT to your total.
[Reference]
Related
my form looks like this:
My problem is, that he "LeistungsBezeichnungsRef" isnt filtering. In my main form, I selected "13222", in addition i want to be shown at the subform just "13222" positions, not 20455, too.
This is my diagram:
The Idea is, that I got a cost centre (13222) to these cost centres it exists some Bills ( excavator, crane etc ) These Bills are cost centre specific! So i build up a table called "LV", where i can type in a position, amount, value etc and in " Leistungserfassung ", I can select a specific position and associate it with a Date/Daily report Number etc , selected in table "Räumstelle".
The field "LeistungsBezeichnungsRef" ( called like "position billing" in engl. ) Has this query:
I already wrote the Combobox-Fieldname of the mainform into the query criteria.. but its not filtering
In the AfterUpdate of the search ComboBox you must requery the subform
Private Sub SearchComboBox_AfterUpdate()
TheSubform.Requery
'Filter the combo in the subform
TheSubform.Form!TheLVCombo.RowSource = "SELECT whatever FROM LV WHERE ID = " _
& SearchComboBox.Value
End Sub
I have a simple MS Access database with shows details about complaints received. I have 4 tables which are as follows:
1st Table is called "tbl_Data" which shows generic info like Complaint Detail etc,
2nd Table is called "tbl_Tools" which lists all Tool numbers and descriptions,
3rd Table is called "tbl_Products" which lists all Product numbers and descriptions,
4th Table is called "tbl_Material" which lists all Material numbers and descriptions.
I want to have 2 Combo Boxes in the frm_Complaints which gets the PartNumber from the correct table depending on the data in the 1st Combo Box.
For example: The 1st Combo Box will list "Tool", "Product", and "Material" depending on what the complaint refers to, and if I select "Tool", the 2nd Combo Box will lookup "PartNo" in "tbl_Tools" only. I have tried Union Queries to merge all 3 tables, but then the data cannot be edited. Is there a simple way in VBA to make the 2nd Combo Box only lookup Part Numbers from the correct table, and then have a 3rd field which shows the corresponding description?
See screenshot link:
As my comment, you can set 2nd ComboBox.List trigger by ComboBox1_Change event.
Private Sub ComboBox1_Change()
Select Case ComboBox1.ListIndex
Case 0
ComboBox2.List = ArrayTool
Case 1
ComboBox2.List = ArrayProducts
Case 2
ComboBox2.List = ArrayMaterial
End Select
End Sub
Or something like this:
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "Tool"
ComboBox2.List = ArrayTool
Case "Products"
ComboBox2.List = ArrayProducts
Case "Material"
ComboBox2.List = ArrayMaterial
End Select
End Sub
You can definitely change the table you select using the method outlined above
- not just pick different arrays -use select case in VBA, and put the SQL into VBA, exactly as you would write it in Access, but within speech marks. In the below, the case changes, on the basis of a change in value of a text box (Me.Poltxt)(not a combo box), and this code sits within a longer sub routine that is triggered by the update of a combo box not mentioned here - but the principle is just the same.
Elements 1 and Elements 2 are the 2 different tables.
Select Case Me.Poltxt.Value
Case "Pol1"
Me.Polcbo.RowSource = "SELECT [Elements 1].[ElemName] FROM [Elements
1] ORDER BY [Elements 1].[ElemName];"
Case "Pol2"
Me.Polcbo.RowSource = "SELECT [Elements 2].[ElemName] FROM [Elements 2] ORDER
BY [Elements 2].[ElemName];"
Case Else: Me.Polcbo.Value = "N/A"
End Select
Part of my access application does invoicing. I have an invoice number table that wholes one field "InvoiceNum". On my invoice report I have the following code:
Private Sub Report_Open(Cancel As Integer)
'lookup invoice number when invoice opens
intInvoiceNum = Nz(DLookup("InvoiceNum", "tblInvoiceNum"), 0)
End Sub`
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
'Incrementally add one (1) to the invoice number for every page of in the report
Me.txtInvoiceNum = intInvoiceNum
intInvoiceNum = intInvoiceNum + 1
End Sub`
Private Sub Report_Close()
'Update tblInvoiceNum with the last invoice number in the report
db.Execute ("UPDATE InvoiceNum SET InvoiceNum = " & intInvoiceNum)
End_Sub`
Problem: My report open with groupby ClientID and ProjectNum. Multiple invoices print on the same report. If any invoice spills over to second page the Format Code runs again inserting an incremental invoice number to the second page. How can I prevent this from happening?
Consider using a Running Sum control with the InvoiceNum in the report's Page Header section. No VBA required.
First, add an invisible textbox for running count over all report's records with following:
Control Source: =1
Running Sum: Over All
Name: RunningCount
Visible: No
Second, add another invisible textbox for DLookUp of InvoiceNum:
Control Source: =DLookup("InvoiceNum", "tblInvoiceNum")
Running Sum: No
Name: intInvoiceNum
Visible: No
Finally, add a visible textbox for sum of prior two controls serving as the incremental Invoice Number:
Control Source: =[intInvoiceNum] + [RunningCount]
Running Sum: No
Name: IncrementalInvoiceNum
Visible: Yes
You may be wondering, why the first two invisible controls? Reason being is using Running Sum: Over All will include both the counts and invoice number for each record of the report (e.g., 1 + InvoiceNum - 1st record; 1 + 1 + InvoiceNum + InvoiceNum - 2nd record...)
I am using LightSwitch (Learning..) with VB.Net and this is my question:
I have some tables called tOrder and tProduct.
I made a computed property in tOrder that has UNITPRICE and TOTALPRICE.. Total Price was easy to made:
Private Sub totalPrice_Compute(ByRef result As Decimal)
result = quantity * unitPrice
End Sub
The problem is with that unitPrice. I can not find a way to assign automatically the value of Price in tProduct according of the user selection. Lets say that in tProduct there are 3 products. Product A with a price of 5, Product B with a price of 10 and Product C with a value of 20. I need that in a screen of "New Order", according the selection of the user (If the user wants ProductA/Product B/Product C) that the UnitPrice in tOrder changes automatically for the user to see the real price of Price in tProduct.
I tried with:
Private Sub unitPrice_Compute(ByRef result As Decimal)
result = Me.tProduct.price
End Sub
But an error appears saying: NullReferenceException was unhandled by user code
Also I tried:
Private Sub unitPrice_Compute(ByRef result As Decimal)
If Me.tProduct.nameProduct <> Nothing Then
result = tProduct.price
Else
result = 0
End If
End Sub
But same error..
I don't know how to solve it, or where, when, how.. I am new in LightSwitch and I will be so grateful if you help me..
Thanks a lot!
Your code is being called before tProduct actually has a value, so tyring to reference its Price property causes an error.
You were very close with your second piece of code. It just needs to be:
Private Sub unitPrice_Compute(ByRef result As Decimal)
If (Me.tProduct IsNot Nothing) Then
result = Me.tProduct.price
Else
result = 0
End If
End Sub
You should always check for null (or Nothing in VB), in other words that an entity has a value, before using any of its properties. Also you can't use <> in a comparison with Nothing, you have to use Is or IsNot.
A simpler alternative would be to write the code like this (although the above version is fine too):
Private Sub unitPrice_Compute(ByRef result As Decimal)
result = If(Me.tProduct Is Nothing, 0, Me.tProduct.price)
End Sub
I am making a till system for a project in access and I have hit a block.
Background
There are numerous forms covered in buttons. clicking a button adds data to a table (TblCurSale) including description and price of item. each form also has a "total" button which sends you to the payment screen, doing so copies the data from the TblCurSale to another table (TblCalc)
TblCalc has columns SaleID, Item(name of item), SalePrice. the report auto adds the sale price column
The total form has two sub reports on it, the TBLCurSale and TblCalc.
On the total screen there is a text filed in which users an input money and then press pay which inputs that figure into the TblCalc as a negative number and then refreshes the page so the new total comes up. at the bottom of the subreport.
Problem
I need an IF vba code so that I can put it so that when the total of the SalePrice column <= 0 I can run a few lines of code. what I have so far is below, so any help would be greatly appreciated.
Private Sub Pay_Click()
Dim SQLPay As String
Dim SQLToTable As String
Dim SQLMoney As Variant
SQLPay = "INSERT INTO TblCalc(SalePriceTotal) VALUES (-'" & TxtPayment & "')"
SQLToTable = "INSERT INTO TblTotalSale (CurrentSaleID, SalePrice, Item) SELECT CurrentSaleID, SalePrice, Item FROM TblCurrentSale"
SQLMoney = "IF (SUM(SalePriceTotal) FROM TblCalc) <= 0 SELECT '1' ELSE '0'"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLPay
DoCmd.RunSQL SQLMoney
If SQLMoney = 1 Then
DoCmd.RunSQL SQLToTable
Me.TxtPayment = ""
Me.Refresh
DoCmd.OpenReport "rptCalc"
Else
Me.TxtPayment = ""
Me.Refresh
Me.Refresh
End If
DoCmd.SetWarnings True
End Sub
I think you would be better to structure it like a transaction table. So instead of inserting the payment into a separate table, add another row to the TblTotalSale with an Item description of "Payment" and the value as a negative number. You can then simply sum the SalePrice column to give you the balance outstanding. It also allows you to record multiple payments against the one sale.
wrt to the negative sale, I think you should put some validation code on your form to prevent users from entering negative item prices, (in the beforeInsert and beforeUpdate events on the form)