Invalid Syntax in Query (1 Viewer)

SteveJtoo

Registered User.
Local time
Today, 04:58
Joined
Sep 26, 2012
Messages
50
Error I'm getting 'The expression you entered contains invalid syntax, or you need to enclose your text data in quotes.

I can't figure out where it's wrong
Code:
If Dcount(Nz([StatFlag]),[Books],[StatFlag]="W") >0 , ((Nz([StatFlag])) ="W" ,  (((Nz([StatFlag]))<>"R" And (Nz([StatFlag]))<>"M" And (Nz([StatFlag]))<>"H" And (Nz([StatFlag]))<>"P") AND ((Authors.Status)="A"))

Can anyone show me my mistake or if it's even possible to do? Thanks
 

plog

Banishment Pending
Local time
Today, 03:58
Joined
May 11, 2011
Messages
11,653
First, that's a hunk of a mess of code. When you get a statement that complex its time to move it to a function inside a module. There's no shame in using more than one line to evaluate something.

Second, is this in a Module? 'If' is used in VBA, but 'Iif' should be used in queries. Where is this code at exactly?

Third, every argument of DCount is a string, none of yours are, the third one's close, but that's because you have a literal string in it. Every argument of Dcount should be surrounded by quotes. The third argument should look like this:

"[StatFlag]='W'"

Fourth, the first argument of DCount has to refer to a field in the table you are referencing. That means you can't put a NZfunction around it, it has to just be this:

"[StatFlag]"

You have more issues after that, but I'm lost with all those parenthesis and commas. I only see one 'If', but it looks like you are trying to nest them. If so, you need more 'If's (or 'Iif's if this is the query). Again, if not, this should be in a function in a module on multiple lines so its more easily read and debugged.
 

spikepl

Eledittingent Beliped
Local time
Today, 10:58
Joined
Nov 3, 2010
Messages
6,142
To add to plogs comments: When a 747 is built, it is not so that they just assemble the entire pile of parts, go off for a flight and hope everything works. But that is exactly what you did.

Instead, whatever it is that you code, make a small bit, make sure it works, and then add another small but tested working bit to it. Not the other way around. You'll find that this approach of many small steps is much faster than one big splashy hop into the doodoo.
 

SteveJtoo

Registered User.
Local time
Today, 04:58
Joined
Sep 26, 2012
Messages
50
Thanks for advise. This is going as an expression in a Query. I have shortened it to test as spikspl suggested and fixed the reference and the Nz and came up with this.

Where (If (Dcount("[Author]","Books","[StatFlag]='W'")) >'0' ([StatFlag])="W")

This is how it looks in SQL but error Invalid use of '.','!',or '()'. in Query expression '(If (Dcount("[Author]","Books","[StatFlag]='W'")) >'0' ([StatFlag])="W"'.
 

Users who are viewing this thread

Top Bottom