Hi csumb
I'm going to make some assumptions about your database structure. If my
assumptions are incorrect then you should redesign your database so that my
assumptions *are* correct <g>
You have the following tables:
Customers
========
cstID (autonumber, primary key)
cstName
.... other customer details - address, phone, etc
Products
=======
prdID (autonumber, primary key)
prdName (e.g "Mug")
prdUnitName (e.g. "ea")
prdUnitPrice
prdUnitsOfSale (e.g. 12)
Orders
=====
ordNumber (primary key)
ordCustomer (CstID of the customer placing the order)
ordDate
ordStatus (new, packed, despatched, completed, cancelled, etc)
OrderItems
========
odiOrder (order number of the related order)
odiProduct (PrdID of the product being ordered)
odiUnits (number of units on order)
odiUnitPrice
odiDiscount (percent discount)
You have set up the following one-to-many relationships:
cstID -< ordCustomer
ordNumber -< odiOrder
prdID -< odiProduct
Now, you say you want a button on your form for each product. Of course you
don't want to have to modify your form design every time you add a new
product, so you do this with a continuous subform bound to the Products
table. Each record has a textbox bound to the PrdName field that *looks*
like a button (raised, centred text, etc, and locked).
You also have a standard form/subform for the Orders and OrderItems (similar
to that in the NorthWind sample database). You place your "product
selector" subform also on this main order form (or maybe it would work
better as a popup). Let's say the main form is named frmOrders and the
OrderItems subform is in a control named sbfOrderItems.
Now, all you need is a Click event procedure for the "button" textbox on
your product selector form. This procedure must do the following:
1. Search for a record in sbfOrderItems corresponding to the product being
clicked.
2. If there is none, create a new record, filling in odiProduct and
odiUnitPrice and setting odiUnits to prdUnitsOfSale.
3. If there is already a record for that product, increase odiUnits by
prdUnitsOfSale.
Something like this:
Private Sub txtProductButton_Click()
Dim f as Form
Set f = Forms!frmOrders!sbfOrderItems.Form
With f.RecordsetClone
.FindFirst "odiProduct=" & Me.prdID
If .NoMatch then
.AddNew
!odiProduct = Me.prdID
!odiUnitPrice = Me.prdUnitPrice
!odiUnits = Me.prdUnitsOfSale
.Update
f.Requery
.FindFirst "odiProduct=" & Me.prdID
Else
!odiUnits = !odiUnits + Me.prdUnitsOfSale
End If
f.Bookmark = .Bookmark
End With
End Sub