IIF syntax

Hi All,
I am getting a syntax error for the following IIF statement? Can you please help me out?
IIF(var_1 = 'XXX',ltrim(ID,0),
IIF(var_1= 'AAA'

AND substr(ID,8,10)='SPD',lpad
(ID,9,'0'), lpad(ID,6,'0'),
IIF(var_1= 'YYY',lpad(ID,9,'0'),ID)))
what I am trying to do is, if var_1 = 'AAA' and the ID contains the string 'SPD'(eg:1234567SPD) then I wanted to lpad the ID with 9,0 otherwise(eg:157050) lpad(6,0).
ANy idea?
thanks.

I don't know if we can use AND ind IIF, to make sure this part of the code is wrong, insert a stupid condition, which you're sure is valid. This can isolate the problem.
But, which part of the code the error is referencing?
----- Original Message ----

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.