Results 1 to 9 of 9
  1. #1
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36

    Format Formula - Text as Number

    I imagine this will be pretty easy but I can't get it to work.



    I have fields with any possibility for data; numbers, alphas, characters, spaces, whatever.

    I want to write a formula that will grab the first three characters of the string only if those three characters are numbers. If the first 3 characters are not numeric, I want to return a blank. My first attempt was:

    Format(Left([Text],3),"#")

    The format formula seemed to have no affect. It would return the first three characters if they were numbers or alpha characters. So then I tried:

    IIf(Format(Left([Text],3),"#")<1,"",Format(Left([Text],3),"#"))

    That was closer but gave me #Error for anything that wasn't numeric. Instead of #Error, I want blanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    What do you want if the value is like "______8797dkjhlkjf" - the underscores represent spaces.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36
    Good question (just when I think I covered all the scenarios ).

    For that I would also want to return a blank.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    I should have asked: So if any value starts with any number of spaces, return a blank even if spaces are followed by numbers?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36
    Correct. I only want the first three characters if they are numeric. All other scenarios I want to return a blank.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Crud! As far as I can tell, this requires checking each of the first 3 characters individually and determining if ALL are numerals.

    Try:

    IIf(IsNumeric(Left(x,1)) AND IsNumeric(Mid(x,2,1)) AND IsNumeric(Mid(x,3,1)), Val(x), Null)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36
    Hmmm. It seemed to work but I had one record return with:

    2012

    If it was working correctly I would have expected it to return:

    201

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Then


    IIf(IsNumeric(Left(x,1)) AND IsNumeric(Mid(x,2,1)) AND IsNumeric(Mid(x,3,1)), Left(x,3), Null)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36
    Perfect, thank you!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 10-18-2013, 01:56 PM
  2. Replies: 10
    Last Post: 09-21-2012, 09:00 AM
  3. Number Format to General Format
    By mkc80 in forum Access
    Replies: 2
    Last Post: 05-05-2012, 02:10 AM
  4. Formula to assign a number to a field value?
    By dashingirish in forum Queries
    Replies: 2
    Last Post: 02-23-2012, 01:21 PM
  5. Field switching from Number format to Text
    By COforlife in forum Queries
    Replies: 1
    Last Post: 11-10-2009, 03:23 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