Solved syntax error in DLookup

cheberdy

Member
Local time
Today, 15:27
Joined
Mar 22, 2023
Messages
77
I have this code: Unit = DLookup("Unit", "Article", "Article number=" & Article & "OR EAN=" & Standard). I get an error syntax error(missing operator). But I do not know why. Standard and Article are strings.
 
"OR EAN="
Is missing a space:
" OR EAN="

Use this before the Dlookup
Debug.Print "Article number=" & Article & "OR EAN=" & Standard

Check if you're getting numbers there, if you're getting strings, you need to add apostrophes. I just read those are strings, then try this:
DLookup("Unit", "Article", "Article number='" & Article & "' OR EAN='" & Standard & "'")
 
Last edited:
Strings need to be surrounded by single quotes.
Put the criteria into a string variable and debug.print it untill you get it right.
Also need spaces between words.
 
All the above plus you need to bracket [Article number]. It's why you only use alphanumeric and underscores in names.
 
Using My magical tool (Smart Domain Functions Builder V1.0) : :)
Code:
DLookUp("[Unit]","[Article]","[Article number] ="& Article &" Or [EAN] ="& Standard &" ")

The tool can be found here :
Smart Domain Functions Builder V1.0

Here how I used it :
1681828181113.png

more details in the above link ;)
 
I didn't download the tool but I have a question. Are you properly delimiting the expressions? Once you allow both AND and OR operators, it is imperative that you use parentheses to control the order in which an expression is evaluated.

For example:
a and b or c or d
Evaluates to:
(a and b) or c or d
Which is NOT the same as:
a and (b or c or d)
Actually I haven't examen that but most the time it works fine with me.
If I face any such case I will fix the tool then. :)
Try the tool and feedback me please.

Also I don't know if your example is explaining in real the expressions syntax.
 
I have this code: Unit = DLookup("Unit", "Article", "Article number=" & Article & "OR EAN=" & Standard). I get an error syntax error(missing operator). But I do not know why. Standard and Article are strings.

Unit = DLookup("[Unit]", "Article", "[Article number] ='" & Article & "' OR [EAN] ='" & Standard & "'")

Since you're using strings you need to use the single quote ' character to surround the strings

If it helps, if Article were "12345" and Standard were "67890", the criteria portion of the Dlookup function would essentially be creating the string below

[Article number] = '12345' OR [EAN] = '67890'
 
a+b*c+d
evaluates to:
a + (b * c) + d
You might have meant:
(a +b) * (c +d)
or
a + (b * (c + d))
Thank you for alerting me to this point, I will try to fix it.
Can you give me the appropriate method when the two conditions ("and" / "or" ) combined
 
Can you please explain how you decided where to put them.
I told the code to put parentheses around the two criterias if it is ("or") between them, and if there are three or four criterias and ("or") between them, it puts the parentheses in front of the first criteria and at the end of the last criteria.

Is that correct ?
 
Examples :
Cr = Criteria
Cr1 Cr2 Cr3 Cr4

( Cr1 or Cr2 ) and Cr3 and Cr4
Cr1 and ( Cr2 or Cr3 ) and Cr4
Cr1 and Cr2 and ( Cr3 or Cr4 )
(
Cr1 or Cr2 or Cr3 ) and Cr4
( Cr1 or Cr2 or Cr3 or Cr4 )
(
Cr1 or Cr2 ) and ( Cr3 or Cr4 )

That's how I've done it.
 
I don't see how you have allowed the user to define where they go.
The tool also allows the user to modify the syntax and test it, through direct modification in the yellow box.
Try it yourself. :)
 
I can see that the tool is not giving the user Every thing in the Final shape but It did most the job for him, Also it give him a chance to modify the given result as he wish. Okay I will make some little modification to the tool to give the user the choice of adding the parentheses where ever he want .

Thank you
 

Users who are viewing this thread

Back
Top Bottom