I have a button at the end of each record on a continuous form and it needs to do this:
Private Sub Update_Click()
Dim SellP As Double
Dim BuyP As Double
Dim Profit As Double
SellP = DLookup(SellPrice, Flips, [Current])
BuyP = DLookup(BuyPrice, Flips, [Current])
Profit = SellP - BuyP
Flips.Profit = Profit
End Sub
Now I know this isn't the correct code but I hope it will give you an idea of what it needs to do, essentially:
Find the SalePrice, find the BuyPrice, minus the BuyPrice from the SalePrice and make the result Profit, then populate the Profit field with profit..
Thanks!
The columns of the current record of the bound table/query are directly available in the code.
You can just write e.g.
Profit = SalePrice - BuyPrice if these fields are all part of the bound data.
You might then move this code in the "AfterUpdate"-Event of both the SalePrice- and the BuyPrice-Textfields, maybe like this:
If IsNull(salePrice) Or IsNull(buyPrice) Then
Profit = 0
Else
Profit = salePrice - buyPrice
End If
Related
I have a project where I connected an Access Database through a DataGridView. I've made some queries based on info inputed by the user through textboxes and comboboxes. Now I need to find a way to count the Average of the records found after the query from one specific column. Is there a way to do that ?
Store the counts from your queries...
Dim lstCounts As New List(Of Integer)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah'
lstCounts.Add(<above result>)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah1'
lstCounts.Add(<above result>)
'Your database retrieval method: SELECT COUNT(*) FROM table WHERE field = 'Blah2'
lstCounts.Add(<above result>)
'etc.
Find the average...
Dim nTotal As Integer = 0
Dim dAverage As Decimal = 0.0
For i As Integer = 0 to lstCounts.Count - 1
nTotal += lstCounts(i)
Next
'Make sure you aren't dividing by zero
If lstCounts.Count > 0
dAverage = nTotal / lstCounts.Count
End If
You could also simply just add the total as you perform each query and not bother using a List, but then you need to track how many queries you ran.
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'm working on an access database at the moment, where I have multiple fields.
Product Quantity (Entered by User)
Product Price (Retrieved by Database)
Total Price (Product Quantity * Product Price)
Discount
Discount needs to be calculated, however I don't know how to set it to 30% on the condition that TotalPrice is more than 50. It would be useful if the TotalPrice automatically updated if it was more than 50 with the discount too.
Set up a new query that brings in Product Quantity, ProductPrice and TotalPrice. Then, in the Design View of the query, add this field:
Discount: IIF((ProductQuantity * ProductPrice) > 50, 30, 0)
If you're not familiar with IIF statements, the above reads: "If ProductQuantity times ProductPrice is greater than 50, then set Discount = 30, otherwise set Discount = 0"
This will set your discount = 0 if Total Price is less than or equal to 50, so edit that last part if it needs to be something else. Also, I made assumptions on your field names, so you may need to tweak those too, but you get the idea.
If you need Discount to actually reflect 30% of Total Price, then if would look like this:
Discount: IIF((ProductQuantity * ProductPrice) > 50, (ProductQuantity * ProductPrice) * .30, 0)
If this is a data entry screen You can add an After_Update Event to Sandwich Price And Sandwich Quantity
Private Sub SandwichPrice_AfterUpdate()
update_total
End Sub
Private Sub SandwichQuantity_AfterUpdate()
update_total
End Sub
Private Sub update_total()
Dim total AS Double
If IsNull(Me.SandwichPrice) OR IsNull(Me.SandwichQuantity) Then Exit Sub
total = Me.SandwichPrice * Me.SandwichQuantity
SELECT CASE total
CASE 51 to 1000
Me.Discount = 0.3
CASE Else
Me.Discount = 0
END SELECT
Me.TotalPrice = total - (total * Me.Discount)
End Sub
Doing it this way will allow you to add multiple tiers of discount by adding CASE Statements i.e. You could add CASE 10 TO 20 with a new discount and it will apply when TotalPrice is >=10 And <= 20.
This will allow your lookup query to return the correct values from discount. I don't think this should be handled in the lookup query but rather in the data storage itself.
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)