Question

I have a database for a carpet company. I have a form which enables the user to make a quote, including choosing the customer, etc. There is a also subform dealing with the products and extras, with quantities and unit prices. To state whether the user requires fitting there is a checkbox. When ticked I would like fitting to be added in the subform as one of the products. Products are normally manually chosen using a dropdown.

Important properties are:
Form name: Orders
Subform name: Order Details Subform
Checkbox name: Fitting
Field name for products: Product ID
Subform linked to table: Order Details
Form linked to table: Orders

I'm assuming VBA is needed, or the macro builder.

Anyway thanks in advance!!

Was it helpful?

Solution

On the check box control, you are going to add an [Event Procedure] on the After Update event for the checkbox (see the properties window). This will stub out the VBA code, click on the ellipsis ("...") to get to the VBA code.

You can use Remou's code (after a fashion) to insert the new Product:

If Me.Fitting Then
    strSQL="INSERT INTO [Order Details] (ProductID,OtherTextField) " & _
        "Values (" & _
        Me.ProductID & ",'" & Me.OtherTextField & "')"
    CurrentDB.Execute strSQL, dbFailOnError

    Me.[Subform control name here].Form.Requery
End If
'[code not tested]

However, you probably also want to check that the Product doesn't already exist in for the Order (does your business rules allow for multiple fittings to be scheduled), and you might also want to allow the Fitting product to be removed when it is unchecked - you could do that with an else condition on the if before the "end if":

if 'blah
     'blah blah
else
    strSQL="Delete [Order Details] " & _
        "where ProductID = " & Me.ProductID & " " & _
            "and OtherTextField = 'fitting' " 
    CurrentDB.Execute strSQL, dbFailOnError

    Me.[Subform control name here].Form.Requery
end if
'[code not tested]

You will also have to hack your Products sub form so that if you delete the Fitting product from it, you update the check box (or so that you can't delete Fittings that way).

Then again maybe you don't want to use a check box, and just have the fitting as being another drop down option, as the other products are. You could always make sure it's auto-populated into [Order Details] whenever a new Order is created so it can't be forgotten. This would be more consistent with the rest of the product selection user interface.

OTHER TIPS

I think the easiest way is to use an append query.

If Me.Fitting Then
   strSQL="INSERT INTO [Order Details] (ProductID,OtherTextField) Values ("
         & Me.ProductID & ",'" & Me.OtherTextField & "')"
   CurrentDB.Execute strSQL, dbFailOnError

   Me.[Subform control name here].Form.Requery
End If

This is the no code solution:

Get rid of the Fitting check box. Make fitting a product and add it like all the rest. If you want to know if an order requires a fitting (I'm guessing that's why you have the checkbox.), you can create a query to see if fitting is one of the products on your reports/invoices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top