DLookup Problem !

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a subform that contains product orders. I want to look up the price
in a Products table. Item-Name is the key in the products table and is a
text field.

Here is the code I am using in the afterupdate event when a user selects
item-name from a combo box control in the orders subform:

Me!Price = DLookup("Price", "Products", "Item-Name='" & Me![Item-Name] & "'")

I keep getting an error message :
Run-time error '2001' You canceled the previous operation

What am I doing wrong here?

Thanks for any help you can give me!

Meryl
 
The message indicates that something went wrong with the DLookup(). Usually
this means that one of the names is incorrect, or the Criteria string is
mal-formed.

Add square brackets around the field name that contains a non-alpha
character. Also, use a string for the 3rd argument, so you can see if it is
formed correctly by printing it to the Immediate Window (Ctrl+G).

Dim strWhere As String
strWhere = "[Item-Name] = """ & Me.[Item-Name] & """"
Debug.Print strWhere
Me!Price = DLookup("Price", "Products", strWhere)
 
Allen,

THANK YOU! Once I added the square brackets and it corrected the problem.

Meryl

Allen Browne said:
The message indicates that something went wrong with the DLookup(). Usually
this means that one of the names is incorrect, or the Criteria string is
mal-formed.

Add square brackets around the field name that contains a non-alpha
character. Also, use a string for the 3rd argument, so you can see if it is
formed correctly by printing it to the Immediate Window (Ctrl+G).

Dim strWhere As String
strWhere = "[Item-Name] = """ & Me.[Item-Name] & """"
Debug.Print strWhere
Me!Price = DLookup("Price", "Products", strWhere)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Meryl said:
I have a subform that contains product orders. I want to look up the price
in a Products table. Item-Name is the key in the products table and is a
text field.

Here is the code I am using in the afterupdate event when a user selects
item-name from a combo box control in the orders subform:

Me!Price = DLookup("Price", "Products", "Item-Name='" & Me![Item-Name] &
"'")

I keep getting an error message :
Run-time error '2001' You canceled the previous operation

What am I doing wrong here?

Thanks for any help you can give me!

Meryl
 
I have a subform that contains product orders. I want to look up the price
in a Products table. Item-Name is the key in the products table and is a
text field.

Here is the code I am using in the afterupdate event when a user selects
item-name from a combo box control in the orders subform:

Me!Price = DLookup("Price", "Products", "Item-Name='" & Me![Item-Name] & "'")

Is this the actual line causing the error? If so, try enclosing Item-Name in square brackets:

Me!Price = DLookup("Price", "Products", "[Item-Name]='" & Me![Item-Name] & "'")

If not, step through the code and see which is acutally causing the error.


Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
Back
Top