Results 1 to 5 of 5

"Text to Columns" with Access

This is a discussion on "Text to Columns" with Access within the Microsoft Access forums, part of the Question Forums category; Hey all, I have a field in my table in the a format similiar to this: SPGHR_IDR_HOUSTON_WHATEVER I want to ...

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Dallas, TX
    Posts
    316

    Default "Text to Columns" with Access

    Hey all,

    I have a field in my table in the a format similiar to this:

    SPGHR_IDR_HOUSTON_WHATEVER

    I want to write a query that will split this up into 4 separate columns, like the Text to Column" feature in Excel.

    What's the easiest way to do this?

    Steve

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Posts
    62,840

    Default

    Steve

    There are various ways to do this.

    How exactly is the data delimited? Is it by the underscore?

    Is there always only 4 pieces of data?

    If there is you could use a user defined function.

    Perhaps something like this.
    Code:
    Function SplitData(DataField As String, Pos As Long, Optional Delimiter = "_") As String
    Dim arrData
        arrData = Split(DataField, Delimiter)
        SplitData = arrData(Pos)
    End Function
    Which could be used as an expression in a query like this.
    Code:
    GetFirst:SplitData([YourField], 0)
    Note the use of 0, this is because Split returns a 0 based array.
    If posting code please use code tags.

  3. #3
    Board Regular
    Join Date
    Feb 2002
    Location
    Dallas, TX
    Posts
    316

    Default

    Thanks Norie,

    That worked great for the 1st column, I take it I need to write a similiar function for each column, and if so, how is Pos known?

    Steve

  4. #4
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Posts
    62,840

    Default

    Steve

    What the code does is split the data delimiting by "_".

    So for the 2nd column change 0 to 1, for the 3rd column change 0 to 2 etc
    If posting code please use code tags.

  5. #5
    Board Regular
    Join Date
    Feb 2002
    Location
    Dallas, TX
    Posts
    316

    Default

    Norie,

    That works perfect! Thank you!

    Steve

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com