Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Parsing Comma Values


    I have one field that has a comma seperated list. I want to split the list in my query. I found a function below online (Don't recall where, to give credit)
    Code:
    Function QuerySplit(FieldName As String, Delim As String, Position As Integer)
        QuerySplit = Split(FieldName, Delim)(Position)
    End Function
    Using this syntax if the field only has ONE value it throws an error.
    Code:
    Field2: Trim(querysplit([Comeon,",",1))
    I tried to also check if the 1st location was null
    Code:
    Field2: IIF(Trim(querysplit([Comeon],",",1)) IS NOT NULL, Trim(querysplit([Comeon],",",1)), "")
    However this throws an error as well. How can I use the split function to properly parse this field?

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    I'm wondering if this would work


    Code:
    Field2:querysplit([Comeon],",",1)

    Code:
    Function QuerySplit(FieldName As String, Delim As String, Position As Integer)   
    if instr(fieldname,delim)>0
    QuerySplit = Split(FieldName, Delim)(Position)
    else querysplit = somethingorother
    end if
    End Function

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    That did it, thank you @andy49

  4. #4
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    If I try this same syntax with a 2 or 3 it will throw an error again.

  5. #5
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Can you clarify what you mean by a 2 or a 3 Jo?

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    A slightly different version............



    Code:
    Function QuerySplit(FieldName As String, Delim As String, Position As Integer)   
    dim mystr(100) as string
    if instr(fieldname,delim)>0
    mystr= Split(FieldName, Delim)
    querysplit = trim(mystr(position))
    
    
    else querysplit = somethingorother
    end if
    End Function

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    This syntax throws an error of can't assign to array.

    What I meant by 2 or 3 was using
    Code:
    Field2:querysplit([Comeon],",",2)
    
    Field2:querysplit([Comeon],",",3)

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Ok the alternative proposed should sort that

  9. #9
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    The updated syntax gives me an error of can't assign to array on this line
    Code:
    mystr = Split(FieldName, Delim)

  10. #10
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    try this


    Code:
    Function QuerySplit(FieldName As String, Delim As String, Position As Integer)   
    dim myarray() as string
    if instr(fieldname,delim)>0
    myarray= Split(FieldName, Delim)
    querysplit = trim(myarray(position))
    
    
    else querysplit = somethingorother
    end if
    End Function

  11. #11
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    it throws a subscsript out of range error on this line
    Code:
    QuerySplit = Trim(myarray(Position))

  12. #12
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    How long is the text Jo?

    Function QuerySplit(FieldName As String, Delim As String, Position As Integer)
    dim myarray() as string
    if instr(fieldname,delim)>0
    myarray= Split(FieldName, Delim)
    if position<=UBound(myArray) then
    querysplit = trim(myarray(position))
    else
    querysplit = ""
    endif

    else querysplit = somethingorother
    end if
    End Function

  13. #13
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Less than 255 characters. Your most recent post fixed it. Thank you so much for the assistance.

  14. #14
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Another challenge thoroughly enjoyed.

  15. #15
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by andy49 View Post
    Another challenge thoroughly enjoyed.
    I Spoke to soon, lol. Further testing reveals that if there is only 1 value in the cell then the value is not displayed (works perfect for all other instances) it will show blank. For example, this returns a false blank if there is no comma in the row
    Code:
    Field:querysplit([Comeon],",",0)

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

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2014, 03:18 PM
  2. Replies: 0
    Last Post: 03-11-2013, 07:57 PM
  3. Replies: 3
    Last Post: 03-20-2011, 04:45 PM
  4. Parsing A Comma Delimited Field
    By AccessGeek in forum Import/Export Data
    Replies: 6
    Last Post: 02-03-2011, 01:52 PM
  5. Replies: 1
    Last Post: 08-21-2009, 06:52 AM

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