Results 1 to 2 of 2
  1. #1
    hmcquade is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    11

    Calculating field from look up table


    Hi there,

    I have a form showing printing information. I have a field called 'Quantity', one called 'Papersize' and one called 'PrintCost'. The PaperSize field is a combo, that takes its values from another table called PrintCosts. This table shows different types of paper size, with a corresponding 'Cost' field showing price per sheet. After the papersize has been chosen and quantity has been updated, i would like the PrintCost field to automatically be updated by taking the papersize and multiplying it by the quantity and come up with a total print cost by looking at the PrintCost table. I have tried this in the Quantity afterupdate event:

    Dim dblCost As Double
    dblCost = Me.Quantity * Nz(DLookup("[Cost]", "PrintCosts", "[PaperSize] = '" & Me.PrintSize & "'"), 0)
    Me.PrintCost.Value = dblCost

    I am getting: "Run-Time error 2001: you cancelled the previous operation"

    Any ideas?

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Welcome to the forum!

    First, you would use different code, and second, you should not store the calculated cost (PrintCost) in the table.

    You would, however, need to store the price, quantity and paper size (or a foreign key that refers to it). I am assuming that your prices may change but that you would want to store the price at the time you create the record.

    On your form, you would have bound controls for quantity (texbox), paper size (combo box) and price and an unbound control for the cost.

    For the combo box, set the row source such that it includes the price in addition to the other fields you want. In the after update event of the combo box, populate the price control with the column of the combo box that holds the value.

    For example if the combo box's row source was as follows, you can reference the cost field.

    SELECT papersizeID, txtPaperSize, cost FROM PrintCosts

    The code in the after update event of the combo box would be like this:

    me.costtextboxcontrolname=me.comboboxname.column(2 )

    Note Access counts the columns starting at 0 not 1.

    You would do a simple calculated control for the printcost, but you would not store it since you have all the key information stored that is necessary to calculate it when you need it. (Storing the calculated cost can cause issues with your data integrity if one of the key fields that was originally used in the calculation is changed and the total cost not updated accordingly. For this reason, it is best not to even store it).

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-11-2011, 02:32 PM
  2. Calculating average in table
    By prv in forum Database Design
    Replies: 1
    Last Post: 12-14-2010, 01:35 PM
  3. Calculating from a Table
    By Swarland in forum Programming
    Replies: 3
    Last Post: 11-25-2010, 11:25 AM
  4. Selecting a corresponding table field based on text field.
    By michaeljohnh in forum Programming
    Replies: 5
    Last Post: 10-08-2010, 10:33 AM
  5. Auto Calculating in remaining field.
    By aligahk06 in forum Forms
    Replies: 1
    Last Post: 05-16-2010, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums