?
Solved

Syntax error in DLookup

Posted on 2014-03-13
6
Medium Priority
?
372 Views
Last Modified: 2014-03-13
What is wrong with this code?  

If DLookup("[ProdRef]", "tblProducts", "ProdRef = '" & Me.txtProductID.Column(1) & "'") <> Me.txtTransferredProdRef Then


ProdRef is a text field.

I'm getting a syntax error missing operator.

??

--Steve
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39926523
The DLookup syntax looks OK, but try using NZ to handle nulls:

If NZ(DLookup("ProdRef", "tblProducts", "ProdRef = '" & Me.txtProductID.Column(1) & "'")) <> Me.txtTransferredProdRef Then

Open in new window

0
 
LVL 38

Expert Comment

by:PatHartman
ID: 39926526
Nothing obvious jumps out at me.  I would start by separating the where argument so you build it before using it in the DLookup().  That way you can stop the code and look at what you are actually sending the function as an argument.
0
 
LVL 12

Assisted Solution

by:pdebaets
pdebaets earned 1000 total points
ID: 39926531
Are there any single quotes in your ProdRef field data?

You may want to try

If DLookup("[ProdRef]", "tblProducts", "ProdRef = " & chr(34) & Me.txtProductID.Column(1) & chr(34)) <> Me.txtTransferredProdRef Then

... which surrounds the value in double quotes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 61

Expert Comment

by:mbizup
ID: 39926538
Actually, the statement as you have written it doesn't make sense.

You are looking up ProdRef where ProdRef = txtProductID.Column(1)... which is overkill for simply writing txtProductID.Column(1) without the DLookup.

Are you sure ProdRef is the correct field name in the DLookup?  I would think you might need this (looking up TransferredProdRef  instead of ProdRef, but check the field name):

If NZ(DLookup("TransferredProdRef ", "tblProducts", "ProdRef = '" & Me.txtProductID.Column(1) & "'")) <> Me.txtTransferredProdRef Then

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 1000 total points
ID: 39926567
... and of course if you REALLY ARE trying to lookup ProdRef where ProdRef = txtProdID.Column(1), then you can get rid of the DLookup and simplify your comparison to this (Just compare it directly):

If  Me.txtProductID.Column(1) <> Me.txtTransferredProdRef Then

Open in new window



(As an aside, is txtProductID really a combo box?   If not, the Column(1) is going to cause problems)
0
 

Author Closing Comment

by:SteveL13
ID: 39926767
Yes there were single quotes but also yes,

If  Me.txtProductID.Column(1) <> Me.txtTransferredProdRef Then

sure made it a lot easier.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question