views:

831

answers:

3

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!!

A: 

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
Remou
sorry i'm confused... put it down to my noviceness!!is this made in the vba editor or actually in append query design view. also have you added another text field simply as an example??
This is VBA and the text field is an example to show the delimiters needed for text. You would also need delimiters for dates (#), but dates are tricky, and need formatting as well. You can use the Query Design Window and switch to SQL view, if you want to get an example of an append (insert) query.
Remou
A: 

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.

CodeSlave
i get a compile error with the Me.ProductID section. Not sure what to do...
Hence my comment about "code not tested". Is your field called "ProductID" or "Product ID". In the latter case you might have to change it to something like Me.[Product ID].
CodeSlave
A: 

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.

Jeff O