0
votes

I have an Access form with a text box named Box1.

In this text box, I want to have the name of the last product in my table Products as the default value.

My table product has the fields: P_ID and P_Name.

I have coded in VBA in the form:

Me!Box1.DefaultValue = DLookup("P_Name", "Products", "P_ID = DMax("P_ID", "Products")")

However, there is an error in my code as the textbox displays #Name.

1
Try =DLookUp("P_Name","Products","P_ID = " & DMax("P_ID","Products")) - Kostas K.
i tried and my text box says #Name ? :( - Acces124
Make sure you copied it correctly, it's working on mine. - Kostas K.
Very weird. I copied your exact words and it doesn't work. I still get the #Name? error in my text box. - Acces124

1 Answers

0
votes

The DefaultValue property is a string, so quotes are needed:

Me!Box1.DefaultValue = "'" & DLookup("P_Name", "Products", "P_ID = " & DMax("P_ID", "Products") & "") & "'"