Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172

    Help with IIf and LEN function

    Hi,

    I am having trouble using these functions together in a query. I have a set of data that could be 3 char or could be 4 char. If it is only 3 char, I need to put a zero in front.

    I am trying:

    IIf(Len([MyField]=3,"0"&[MyField],[MyField]))



    But am getting an error saying that my expression has the wrong number of arguments.

    Any help much appreciated.

    Cheers,
    Kirsti

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You don't close the Len function correctly, but I'd do this:

    Format(MyField, "0000")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Check

    IIf(Len([myField])=3,"0" & [myField],[myField])

    Thanks

    Edit : Oops, was typing while pbaldy posted.

  4. #4
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Thank you both - have it working perfectly now.

    Kirsti

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    We were happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    I know I'm a little behind the times here, but I am trying to do something similar. Only I need to add a ".0" to end of any value that is 3 characters long.

    My table is named "Master"
    My column is named "DIAGNOSIS01"

    My SQL Skills are Iffy at best.

    Bust basically If LEN[DIAGNOSIS01]=3 then add ".0" to the end making 111 into 111.0

  7. #7
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Try,

    IIf(Len([DIAGNOSIS01])=3,[DIAGNOSIS01]&".0",[DIAGNOSIS01])

    Good luck!

  8. #8
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    Is that using an update query?

  9. #9
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    No, just an ordinary select query.

    Click image for larger version. 

Name:	Capture.JPG 
Views:	12 
Size:	31.8 KB 
ID:	16950

    It would look like this...

  10. #10
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    Just a little background, when i was given this project it came with 2 problems, both of which had to do with excel stripping data out before it got to the database. Now I get to fix it. the first issue was diagnosis codes come in this format xxx.x or xxx.xx with x being alphanumeric characters. The first problem was it stripped the 0's from the front of the of any value. i.e. 008.45 became 8.45 and 034.25 became 34.25. I used the following sql in an update query to fix that:

    UPDATE Master
    SET DIAGNOSIS01 = LEFT("00", 3-LEN(DIAGNOSIS01)) + DIAGNOSIS01
    WHERE LEN(DIAGNOSIS01)<3 AND Len(DIAGNOSIS01)>0;

    Now, the second problem is it stripped out the .0 from a bunch of values making 111.0 into 111

    I figured that a variation of the above sql using "right" vice "left" and moving the other stuff around would work but I am at a stalemate.

  11. #11
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    OK, I see where the confusion is, I need it to actually permanently update the values... That worked for returning a value with a ".0" on the end, but I need it to update the records on the master table.

  12. #12
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Ok, but the code I posted above could be used in an update query (doesn't have to be a SELECT query). I tested it with dummy data in my DB and it worked. You would need to run it after your update query.

  13. #13
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    So what would the update query look like, because I can't seem to get it to work. Sorry, I must be missing something...

  14. #14
    LUMPKINBD is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    10
    got it... thanks a ton.

    UPDATE Master SET Master.DIAGNOSIS01 = IIf(Len([Master].[DIAGNOSIS01])=3,[Master].[DIAGNOSIS01] & ".0",[Master].[DIAGNOSIS01]);

  15. #15
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Fantastic - glad to hear you got it working!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. HELP with IIF function
    By lpfluger in forum Queries
    Replies: 3
    Last Post: 04-01-2011, 12:01 AM
  2. if function
    By lolo in forum Queries
    Replies: 1
    Last Post: 08-01-2010, 11:38 PM
  3. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM
  4. function key
    By marianne in forum Access
    Replies: 5
    Last Post: 05-14-2009, 01:26 AM
  5. Avg Function
    By hiker8117 in forum Access
    Replies: 3
    Last Post: 04-23-2009, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums