Subject Re: [IBO] IB_Query Ordering Problems
Author Paul Vinkenoog
Hello Kenneth,

> Thanks Paul, I now use the OrderingItemNo to do the job. But I
> still don't understand why I get the "Invalid Token Error" if I use
> the SQLOrder syntax (using the Add method). So, I think I'd better
> avoid that from now on. :(

SQLOrder does work, but it doesn't do any magic. If you assign it or
add to it, you have to make sure that when you're done, SQLOrder
contains a valid clause, e.g.

ORDER BY FRUITCAKES, NUTCASES

Especially if you _add_ instead of assign, you have to be careful.
Suppose the ordering clause is ORDER BY FRUITCAKES and you say

MyQuery.SQLOrder.Add( 'NUTCASES' );

the resulting SQLOrder will be

ORDER BY FRUITCAKES
NUTCASES

which is invalid, because the comma isn't there. On the other hand, if
you add ', NUTCASES' and there wasn't an ordering clause yet, the
result will be

, NUTCASES

which is also invalid.

And this is more or less what you did:

OnSQLPrepare event;
if OrderByName then
SQLOrder.Add('LOC_SHOP, NAME')
else
SQLOrder.Add('LOC_SHOP, ITEMCODE');

As there wasn't an ordering clause yet, the resulting SQL must have
looked something like

SELECT *
FROM MYTABLE
WHERE NAME <> 'NotThisName'
LOC_SHOP, NAME

IBO doesn't check what you put in SQLOrder, but the InterBase/Firebird
parser reports an unknown token as soon as it hits LOC_SHOP - because
at that point in the SQL, it expects a keyword like AND or UNION or
ORDER BY.

If you do:

MyQuery.SQLOrder.Text := 'ORDER BY LOC_SHOP, NAME';

you won't get the error.


Greetings,
Paul Vinkenoog