Results 1 to 6 of 6
  1. #1
    geocan2006 is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    May 2016
    Posts
    34

    Text query before comma if any

    Hi,
    I have a query column which has a text , it may contain words with or without a comma, in the example below I need that it results 1st text string before comma if any or not
    Thanks



    Color 1st color
    Green, Yellow, Red Green
    Yellow, Red Yellow
    Red Red

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Try:

    Left([Color] & ",", InStr([Color] & ",", ",")-1)
    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
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Quote Originally Posted by June7 View Post
    Try:

    Left([Color] & ",", InStr([Color] & ",", ",")-1)
    The & "," is superfluous. More simply it's

    Code:
    Left([Color],Instr([Color],",")-1)
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Did you try it? It will fail if the comma is not present because InStr() returns 0 and then -1 will error in the Left().
    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
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Quote Originally Posted by June7 View Post
    Did you try it? It will fail if the comma is not present because InStr() returns 0 and then -1 will error in the Left().
    Apologies. Only half awake here.
    Of course you are correct. In my version, the final record would fail.

    There are alternatives which involve checking if a comma exists but that makes the solution more complex.
    Your solution does work for all records
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  6. #6
    geocan2006 is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    May 2016
    Posts
    34
    Yess, It Worked.

    Left([Color] & ",", InStr([Color] & ",", ",")-1)

    Thank you

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

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2017, 07:54 AM
  2. Replies: 8
    Last Post: 06-04-2014, 10:41 AM
  3. exporting query to text comma separated
    By Pasi in forum Queries
    Replies: 6
    Last Post: 12-27-2013, 06:42 PM
  4. Replies: 2
    Last Post: 05-16-2012, 04:45 PM
  5. End of Text Box - Replace Comma with Period
    By bburton in forum Programming
    Replies: 14
    Last Post: 06-20-2011, 03:20 PM

Tags for this Thread

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