Syntax mystery

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

I can't figure out the pattern for what constitutes good and bad syntax.
(ls_SQL is a good sql statement)

Dim dbCurr as DAO.database

These statements have good syntax
dbCurr.Execute ls_SQL , dbFailOnError
Call dbCurr.Execute (ls_SQL, dbFailOnError)
dbCurr.Execute (ls_SQL)

This is bad syntax
dbCurr.Execute(ls_SQL, dbFailOnError)
 
The last one fails because you're not catching the return value. The
dbCurr.Execute (Is_SQL) works because VBA is actually treating the
parentheses as operators, not as tokens indicating a function call.

Did that help at all? <g>

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Rebecca Riordan said:
The last one fails because you're not catching the return value. The
dbCurr.Execute (Is_SQL) works because VBA is actually treating the
parentheses as operators, not as tokens indicating a function call.

Did that help at all? <g>

Well.... getting there. Figuring that "catching the return value" meant
assigning it, I tried

li_safety = Currentdb.Execute(ls_sql,dbFailOnError)

and that worked.

OK... So.. trying to derive principles here.... If you use parenthese,
besure to "catch" the return value, if there is one.

The next one is a little harder...... Why would VBA treat the parentheses as
operators instead of tokens indicating a function call? Is the other
principal at work here that the only time you can use parentheses (without
assigning the return value) is when you have only one parameter, because
then VB doesn't think it's a function call???? So is "function call"
different than something else? What does VB differently when you pass
multiple parameters without the parentheses?

In other words. Where are the general rules behind all this.

But thank you very much for moving me along the road. Definitely helpful!

Then
 
Okay, let's back up a bit. I'm sure you already know a bunch of this,
but...

A function is a procedure that returns a value. A sub is a procedure that
performs some action and doesn't return a value. An operator is a special
kind of function. VBA doesn't allow you to write your own operators, but
some languages do. Most operators are single characters, = and + are good
examples. A token is a character that tells the compiler how to interpret
the source code.

In the case of

x = Foo(y, z)

the ( and ) characters are _tokens_ that tell the compiler to send the
values of y and z to the function Foo. The = is an _operator_ that tells
the compiler to store the value returned by Foo in the variable x. That's
all straight-forward enough, I think.

But VBA has their syntactical wierdness in that it allows some procedures to
be called as either functions or subs. So you can say

x = Foo(y,z)

or

Foo x, y

It's convenient, but sometimes confusing, as when you have a single
argument, because the characters () can be both a token that says "pass
these values as arguments" and an operator that says "interpret this String
as an expression". So unless you also give the compiler the clue of the
assignment operator, it gets its wee self confused.

BTW, I don't think the () operators still work as operators; they've been
replaced by the explicit EVAL function.


Does that help?


--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Back
Top