Calculated controls on the Sub Report

Hi all Experts!

I want your help on cost of sales, the second sub report attached to the invoice is supposed to calculate the cost of goods sold. At the moment staff are keying in the calculated average unit cost of the products manually which is prone to errors, and so to try and solve this problem I have created the code below, the idea here is that, the code will first sum all the total invoices values and then divided by the sum of all units for that product bought, the result is supposed to be an average cost per unit. But it is giving an error, any idea?

Private Sub Form_BeforeInsert(Cancel As Integer)
    Me.Cost = (Dsum(“TotalCost”,”tblInventory”,”ProductID = “ &Me.ProductID))/ (Dsum(“QuantityBought”,”tblInventory”,”ProductID = “ &Me.ProductID))
End Sub

Before select the product Id I have also notice an error, it disappears temporary after product ID selection. I understand there is a function in Ms Access called ISERROR(Value)that can suppress this error but I’m not sure how to use it.

Accomplishment

Here what I want to archive is that the moment a product ID is selected, the cost field on the form must be field and once the form was finished and saved then the cost should be inserted in the underlying table. Does this sound possible?

Regards

Chris
LVL 3
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Break up the calculation a bit more so you can check if you have any values to calculate with:

  Dim   curTotalCost as Currency
  Dim   lngNumberOfUnits as Long

  curTotalCost = NZ(Dsum(“TotalCost”,”tblInventory”,”ProductID = “ &Me.ProductID),0)
  lngNumberOfUnits = NZ(Dsum(“QuantityBought”,”tblInventory”,”ProductID = “ &Me.ProductID),0)

 If curTotalCost<>0 and lngNumberOfUnits<>0 then
    Me.Cost = curTotalCost  / lngNumberOfUnits
Else
    Me.Cost = 0
End If

NZ() is "Null to Zero".   What it does is gives you a value back (in this case a 0, which is the default) if a NULL is encountered, which is what DSum() will return if there are no records.

Jim.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.