Private Sub ProductID_AfterUpdate()
' if an error occurs during the execution of this
' subroutine then send perform the procedure that
' the next line points to
On Error GoTo Err_ProductID_AfterUpdate
' create a variable to store the filter we are going to use
' to remind us that it is a string variable we put [b]str[/b]
' at the front of it and call it Filter, hence strFilter
Dim strFilter As String
' Evaluate filter before it's passed to DLookup function.
' Here we are deciding what our criteria will be
' We know that we want to use the field called ProductID
' and we also know we are looking for the ProductID of the
' record in the table where that is equal to the object
' called ProductID
strFilter = "ProductID = " & Me!ProductID
' Look up product's unit price and assign it to UnitPrice control.
' Here we are assigning the field called UnitPrice with a value
' that we are getting from the table. The DLookup is built with
' the following arguments. DLookup(FIELDNAME, TABLENAME, [CRITERIA]) - I've bracketed criteria as it is not always necessary.
' So we are looking for the price in the table called Products where, by using the filter, the ProductID field is equal to the chosen product
Me!UnitPrice = DLookup("UnitPrice", "Products", strFilter)
' will exit the subroutine and is ended with an Exit Sub statement
' so that the code does carry on through and run the error handling
' section underneath
Exit_ProductID_AfterUpdate:
Exit Sub
' the area of code that deals with errors - this will just give a message
' box detailing the nature of the error
Err_ProductID_AfterUpdate:
MsgBox Err.Description
Resume Exit_ProductID_AfterUpdate
End Sub