M
my-wings
I often buy books in lots but sell them individually, and I need to allocate
lot costs over individual books in my database. What I want to do is enter
the total cost of a set of books and a total cost of shipping those books,
then figure the cost per book and place those amounts into the subform
record for each book. Then I want to add the two fields in the subform
record together to give me a single cost for that book. This will be done on
a button click, so there is no need to continuously update the number of
records or averages as I add books to the subform.
Here are the forms and fields I'm dealing with:
frmPurchases (the main form)
CostOfBooks (bound field)
CostOfShipping (bound field)
txtTotalBooks (unbound - this holds the number of records in the
subform and is used
to calculate the txtAvgCost and txtAvgShipping fields)
txtAvgCost (unbound - this will hold an amount to be put into the
subform record)
txtAvgShipping (unbound - this will hold an amount to be put into
the subform record)
sfrmBookList (the name of the control in frmPurchases that holds the
subform)
subfrmBookList (the name of the subform itself)
MyCostWOShipping (bound field - needs to be updated with amount from
txtAvgCost)
MyShippingCost (bound field - needs to be updated with amount from
txtAvgCost)
MyTotalCost (bound field - the sum of MyCostWOShipping and
MyShippingCost
I have been scouring the internet looking for pieces of code that will help
me with this, and I'm halfway there. The part I got works so far, but
somehow I don't think I have the best solution. Here what I have so far,
which counts the records in the subform, puts that number in the
txtTotalBooks field and calculates the average cost and average shipping per
book.
Private Sub btnTotalCostSpread_Click()
Dim frm As Form
Dim dblAvgCost As Double, dblAvgShip As Double
Set frm = Me.sfrmBooks.Form
With frm
.RecordsetClone.MoveLast
Me.txtTotalBooks.Value = .RecordsetClone.RecordCount
End With
Set frm = Nothing
dblAvgCost = Round(Me.CostOfBooks / Me.txtTotalBooks.Value, 2)
dblAvgShip = Round(Me.CostOfShipping / Me.txtTotalBooks.Value, 2)
Me.txtAvgCost = dblAvgCost
Me.txtAvgShipping = dblAvgShip
End Sub
Now I'm stuck as to how to update the records in the subform. I think I need
to (maybe) do something with a RecordsetClone, but I can't find anything on
the internet that looks like what I need. Can anyone help?
Thanks.
Alice
lot costs over individual books in my database. What I want to do is enter
the total cost of a set of books and a total cost of shipping those books,
then figure the cost per book and place those amounts into the subform
record for each book. Then I want to add the two fields in the subform
record together to give me a single cost for that book. This will be done on
a button click, so there is no need to continuously update the number of
records or averages as I add books to the subform.
Here are the forms and fields I'm dealing with:
frmPurchases (the main form)
CostOfBooks (bound field)
CostOfShipping (bound field)
txtTotalBooks (unbound - this holds the number of records in the
subform and is used
to calculate the txtAvgCost and txtAvgShipping fields)
txtAvgCost (unbound - this will hold an amount to be put into the
subform record)
txtAvgShipping (unbound - this will hold an amount to be put into
the subform record)
sfrmBookList (the name of the control in frmPurchases that holds the
subform)
subfrmBookList (the name of the subform itself)
MyCostWOShipping (bound field - needs to be updated with amount from
txtAvgCost)
MyShippingCost (bound field - needs to be updated with amount from
txtAvgCost)
MyTotalCost (bound field - the sum of MyCostWOShipping and
MyShippingCost
I have been scouring the internet looking for pieces of code that will help
me with this, and I'm halfway there. The part I got works so far, but
somehow I don't think I have the best solution. Here what I have so far,
which counts the records in the subform, puts that number in the
txtTotalBooks field and calculates the average cost and average shipping per
book.
Private Sub btnTotalCostSpread_Click()
Dim frm As Form
Dim dblAvgCost As Double, dblAvgShip As Double
Set frm = Me.sfrmBooks.Form
With frm
.RecordsetClone.MoveLast
Me.txtTotalBooks.Value = .RecordsetClone.RecordCount
End With
Set frm = Nothing
dblAvgCost = Round(Me.CostOfBooks / Me.txtTotalBooks.Value, 2)
dblAvgShip = Round(Me.CostOfShipping / Me.txtTotalBooks.Value, 2)
Me.txtAvgCost = dblAvgCost
Me.txtAvgShipping = dblAvgShip
End Sub
Now I'm stuck as to how to update the records in the subform. I think I need
to (maybe) do something with a RecordsetClone, but I can't find anything on
the internet that looks like what I need. Can anyone help?
Thanks.
Alice