Richard:
Don't create a column in the Batches table for the number of items per
batch. The number can be computed on the fly, so its unnecessary to store
it, but more importantly, to do so runs the risk of introducing
inconsistencies into the data.
To show the number for the current batch on the batches form add an unbound
text box with a ControlSource along these lines:
=DCount("*", "Items", "BatchID = " & [BatchID])
This assumes that BatchID is the name of the primary key of Batches and of
the corresponding foreign key column in Items, and that the columns are of a
number data type, not text. If the keys are text data type wrap the value in
quotes like so:
=DCount("*", "Items", "BatchID = """ & [BatchID] & """")
You can also use the DCount function in the same way in a report or in a
query, though in the latter you could also use a subquery, e.g.
SELECT *,
(SELECT COUNT(*)
FROM Items
WHERE Items.BatchID = Batches.BatchID) AS ItemCount
FROM Batches;
You can prevent more than 17 items per batch being created with some
validation code in the Items form. You don't say how Items records are being
created, but if the batch is being selected manually, most likely via a combo
box bound to the BatchID column, then you can put the code in that control's
BeforeUpdate event procedure, e.g.
Const conMESSAGE = "17 items have already been entered for this batch."
If DCount("*", "Items", "BatchID = " & Me.BatchID) = 17 Then
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
Cancel = True
End If
If the BatchID value is being entered automatically when a new record is
created, e.g. by being passed from the Batches form via code which opens the
Items form, then you could either do the validation in the Batches form, so
it only opens the Items form if less than 17 items records already exist for
the batch, e.g.
Const conMESSAGE = "17 items have already been entered for this batch."
If DCount("*", "Items", "BatchID = " & Me.BatchID) = 17 Then
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
Else
DoCmd.OpenForm "Items", DataMode:=acFormAdd
Forms("Items").BatchID = Me.BatchID
End If
or you could use the same code as suggested for the BatchID control on the
Items form above in the form's BeforeUpdate event procedure. Doing the
validation before data is entered in the items form is obviously a better way
to go, however.
Note that validating the number of items per batch at form level does not
prevent more than 17 rows per batch being entered directly into the table, so
you should ensure that users can only enter data via the form.
Ken Sheridan
Stafford, England
richard said:
I have two tables and two forms
1 Batches
2 Items
Items are related to a batch number (number is autonumbering for batch
number and sample number). There needs to be a maximum of 17 items to a batch.
I wish to have a check box / command button which when clicked will create a
field within the table BATCHES with the number of samples relating to the
particular batch number
ie if there are 10 items in a batch then I need this counting and inputing
the number into the appropriate field in the batch.
I thought this may be achieved with an update query which would select the
data based upon the batch number selected count the number of records and put
this information into the field in batches.
The point(s) I am unsure of are
1 Is this the best way of achieving this and if it is
2 How to perform the count part of the query
Many thanks
Richard