Dlookup as a default value (1 Viewer)

Shecky

New member
Local time
Today, 17:59
Joined
Jul 29, 2021
Messages
25
Hello.
I have used the following code as the default value for my subform field "Product_Desc":
Code:
=DLookUp(" [Supplies]![Product_Desc]  "," [Supplies] ",[Supplies]![TP_Prod_Code ]=[TP_Prod_Code])

To put it into words, I would like the default value for my subform field Product_Desc to be the Product_Desc from my supplies table, where the product code from a dropdown box in the subform is the same as the product code in the supplies table. I have tried using the Ctrl-Alt-Space to load the value into the form and nothing happens.
supplies.png

po_details.png


I am not sure if it is a syntax error or another issue.
TP_Prod_Code is an autonum field with a format of 'TPC'0000 on Supplies, and a number on PO_details with the same format. Data in the table appears stored correctly. Table designs are below.

Any comment on why the default value wont load? Records in the PO_details where the TP_Prod_Code combo box has yet to be entered/selected shows a #Name, which seems correct.
supplies_design.png
po_details_design.png
 

Attachments

  • supplies_design.png
    supplies_design.png
    18.3 KB · Views: 429

Minty

AWF VIP
Local time
Today, 22:59
Joined
Jul 26, 2013
Messages
10,371
You shouldn't store the description in both places, on the form simply display the appropriate value from the combo drop down from your prod code value.

And a default value in a table can't reference another table to the best of my knowledge.
You could do it on a form, but only in the after update of the TP_Prod_Code.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:59
Joined
Feb 28, 2001
Messages
27,172
If you are saying that your autonumber is FORMATTED to read TPCnnnn but you are actually storing it as nnnn, then any lookups have to use the "as stored" rather than the "as formatted" value for doing lookups. It might be a pain in the toches to think along these lines, but NEVER use the formatted values for lookup. The only time formatted values should show up would be on forms or reports visible to the end users.
 

Shecky

New member
Local time
Today, 17:59
Joined
Jul 29, 2021
Messages
25
Thanks for the replies people.
I do realize I am storing the data twice, but I need to keep record of what was ordered 'at the time as product descriptions can change, and I also want the user to be able to add a note that might be relevant to that specific order "Red caps only" or something similar.

As Minty suggested I think that maybe this would be better as VBA code on an AfterUpdate once the TPC combobox is selected.

Im concerned the actual error may be due to the way the data is stored as Doc_Man said, hence why I posted the data from the tables.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:59
Joined
May 7, 2009
Messages
19,230
I do realize I am storing the data twice, but I need to keep record of what was ordered 'at the time as product descriptions can change
this must not change?
a product code is tied to a particular product description, 1:1.
if there is any light deviation in color, dimension, material class, you need to make new product code for it.
you don't change it, you create new one.
 

Shecky

New member
Local time
Today, 17:59
Joined
Jul 29, 2021
Messages
25
Arnelgp: No the Prod_Desc must also be stored at the time of issuing of the purchase order. In case the description should change in the future for whatever reason. If there is a dispute regarding the PO, the exact text desc must retained.

I have tried to add code to my AfterUpdate event on the TP_Prod_Code combo box using the macro builder:
[Product_Desc]=DLookUp("Product_Desc","Supplies",[TP_Prod_Code]=[Forms]![Purchase_Orders]![PO_Details Subform]![TP_Prod_Code])

Nothing happens after update:(

Is changing another control by macro possible in the way I am trying? Must I use VBA?
Inkedpo_details_LI.jpg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:59
Joined
May 7, 2009
Messages
19,230
if that is the "path" you want to take, it's ok, it's not my db anyway.
you can use macro or vba, vba would be much easier:

private sub tp_prod_code_afterupdatate()
me.product_desc = me.tp_pro_code.column(1)
end with
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:59
Joined
Feb 19, 2002
Messages
43,257
The most efficient way to show lookup data is to base the form on a query that uses a LEFT join to the lookup table. When you choose the value from the combo, the lookup is activated and the bound controls on the form will populate. Also note that this method does not store duplicate data nor does it require any code at all.

WARNING - when using this method, it is important to prevent accidental updating of the lookup values so always set the the locked property to Yes for all of the lookup fields.
 

Users who are viewing this thread

Top Bottom