Results 1 to 6 of 6
  1. #1
    Searcher is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    4

    Default value in a form coming from another table reliant on 2 other form fields

    I want to be able to populate a field in a form from another table, dependant on what is filled in on 2 previous form fields in the same form.
    I have a company table with details on companies, I have waste-type table with details on several waste types, I have another table with default prices for companies and waste types. The company field and the waste-type field in the default-price table are both lookups from the company and waste-type tables.
    I have a main data entry table, that requires the user to enter the company and waste type from lookup tables and I would like to pull in the default price to a field on the form so the user doesn't have to enter it all the time but can overwrite it if needed.
    I am trying to use
    Private Sub Price_GotFocus()
    SetPrice = DLookup("Price", "Default_Prices", "[Company] = '" & Forms![Liquid_Docket_Data_Entry_with_Prices]![Company] & "'" _
    And "[Waste_Type] = '" & Forms![Liquid_Docket_Data_Entry_with_Prices]![Waste_Type] & "'")


    Liquid_Docket_Data_Entry_with_Prices]![Price] = SetPrice
    End Sub


    There are no errors when I go through the form but nothing populates the price field.
    SetPrice is a global variable set as currency - done in module called Variable_Declaration

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    That all looks good so don't know why it isn't working. If you want to provide db for analysis follow instructions at bottom of my post. Identify objects involved in issue.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Searcher is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    4
    Thanks June7, I have zipped and attached the database. Some of the tables will now look a bit sparse as I have cleaned out some data.
    Attached Files Attached Files

  4. #4
    Searcher is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    4
    Quote Originally Posted by Searcher View Post
    Thanks June7, I have zipped and attached the database. Some of the tables will now look a bit sparse as I have cleaned out some data.
    Tables "Companies" and "Waste_Stream" are the one referenced in the "Default_Prices" table.
    I am using the "Liquid_Docket_Data_Entry_with_prices" form for data entry and would like the price from the Default_Prices table to populate the price field after the company field and Waste_Type field have been selected.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    There is syntax error in the DLookup. The AND operator needs to be within quote marks. This simplified version works:

    SetPrice = DLookup("Price", "Default_Prices", "[Company] = '" & Me.Company & "' And [Waste_Type] = '" & Me.Waste_Type & "'")
    Me.Price = SetPrice

    You have autonumber fields set as primary key in Companies and Waste_Stream but save the text descriptors into Waste_Recieved (received is misspelled). This means the descriptors should be set as the primary keys. However, names can be poor unique identifiers.

    Possible problem with this code is it won't run if user changes company or waste type but does not go to the price textbox. To assure that the correct price is saved in such event, code must be in the AfterUpdate events of Company and Waste_Type.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Searcher is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    4
    Thanks so much, works a treat. I was going around in circles trying to solve that.

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

Similar Threads

  1. relating fields from one table to a form
    By ljmellor in forum Forms
    Replies: 11
    Last Post: 12-06-2012, 03:55 PM
  2. Replies: 1
    Last Post: 03-27-2012, 05:25 PM
  3. Replies: 1
    Last Post: 09-11-2011, 06:14 PM
  4. Default form layout when creating new form
    By MDB in forum Programming
    Replies: 1
    Last Post: 08-30-2011, 08:17 AM
  5. Replies: 2
    Last Post: 09-01-2010, 09:51 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