Results 1 to 10 of 10
  1. #1
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232

    Left / Mid/ Right for a query


    I have a text field that has three lines in the field. I would like to separate the lines in a query I tired the Mid (see below ) which leaves off the first line but the third lines still shows. But I will need to do the same for the third line also.
    Thanks Angie


    Address: Mid([bill to],InStr([bill to],Chr(10)))

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Show us all the text in the field.
    And show us how you'd like it broken out.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If you know you always have three lines separated by chr(10), use the split() function:

    Address: split([bill to], Chr(10))(1)

    Split() returns an array, with the references to the array members being zero-based, to the above example gives you the second line of the [bill to] field.

  4. #4
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    John White
    5933 Castle Dr
    Pace Fl 32570

    I would like in the query to separate into three different fields for a report.
    I would have like Name = first line Address = Second Line and City = Third Line

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Along the lines of what John suggested, you could try the following logic (assuming BillTo is the name of your field)
    Code:
    Dim myData as Variant
    Dim i as integer
    MyData = Split(BillTo,vbCrLf)
    for i = lBound(MyData) to Ubound(MyData)
          select case i
          case 1
             [Name] = MyData(i)
        Case 2
            [Address] = MyData(i)
        Case 3
         [City] = MyData(i)
         case else 
          MsgBox "unexpected data in the input"
       end select
    next i

  6. #6
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    This is for a query where do I put the above code?

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another option...

    In a query, add these columns:
    Code:
    Name: Left([bill to],InStr([bill to],Chr(10))-1)
    Address: Mid([bill to],InStr([bill to],Chr(10))+1,InStr(InStr([bill to],Chr(10))+1,[bill to],Chr(10))-InStr([bill to],Chr(10)))
    City: Mid([bill to],InStrRev([bill to],Chr(10))+1)
    I would advise you NOT to use spaces in object names.
    Instead of
    "[bill to]" (with a space), use
    "BillTo" or "Bill_To".

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Name is a reserved word in Access

    so, in the query try

    sName: split(BillTo,chr(13) & chr(10) ) (0)
    Address:split(BillTo,chr(13) & chr(10) ) (1)
    City: split(BillTo,chr(13) & chr(10) ) (2)

  9. #9
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    Thanks Ssanfu, the above code in a query worked great.

    Angie

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You're welcome.....

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

Similar Threads

  1. Replies: 16
    Last Post: 06-06-2016, 08:45 AM
  2. How to query if date field is left blank?
    By McArthurGDM in forum Queries
    Replies: 9
    Last Post: 08-04-2014, 05:18 PM
  3. Left Join Query Issue - MS Access
    By mkc80 in forum Queries
    Replies: 1
    Last Post: 08-07-2013, 04:17 PM
  4. Left () function in a query
    By Dega in forum Queries
    Replies: 3
    Last Post: 05-24-2012, 03:46 PM
  5. Replies: 3
    Last Post: 02-02-2011, 01:00 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