Results 1 to 4 of 4
  1. #1
    RBusiness is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Feb 2012
    Posts
    3

    Split Up Data in One Field into Several Fields? boblarson solved a similar challenge

    Hello folks,


    I have a field in an Access 2003 table that has several image names in it separated by "; " (semi colon and space). The thing is, I need to split them up into their own fields. I found a similar challenge (link below) but it's not the same so I'm posting this as new.

    My table name is "ONE BIG TABLE" (will be exporting data from one table for CSV)
    My starting field name is "ALT_IMG".
    An example of the contents of a record within "ALT_IMG" is
    Code:
    /AAG70260G05_2_1.JPG; AAG70260G05_3_1.JPG; AAG70260G05_5_1.JPG; AAG70260G05_6_1.JPG; AAG70260G05_7_1.JPG; AAG70260G05_8_1.JPG; AAG70260G05_4_1.JPG
    What I need is to split these up into their own fields. I can create new fields to populate, I just need to get them in the fields and to remove them from the original ALT_IMG field after moved. There can be as many as 0 or 1 to 20 images in the ALT_IMG field.

    Ultimately I was thinking about making new fields named "ALT_IMG_2", "ALT_IMG_3" (up tp 20) and then making an update query of sort to anything more than 1 image to the next field. Meaning if there are two images, then the first stays where it is and the second is moved to ALT_IMG_2. If there are three then the first stays where it is, the second goes to ALT_IMG_2 and the third goes to ALT_IMG_3.


    Now I know that there are benefits of having multiple tables but I need this to end up in the same table. Please let me know what I can do.



    I can go back and clean up the spaces and ; characters after with an update query but if it can be worked in, I'll take it.

    Thank you for your much anticipated response!
    RB



    Similar solution by boblarson

    https://www.accessforums.net/showthr...mber-of-fields

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Try using Bob's solution. You'll have to adjust the field name, and you want to split on "; "
    There is more logic involved I'm sure, but I think that approach should work.

    I'd suggest a new table with some identifier and separate records for each of these values.

    Can you post the table mdb?

  3. #3
    RBusiness is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Feb 2012
    Posts
    3
    Hi Orange,

    The table mdb is about 1GB so no can do. Bob's solution is confusing me but not because of Bob's post but because I'm not fluent in any of this. I've had great success and have made 50+ queries for updates, table joins and so many more things. This one though, which is my last stem is an all day event so far.

    I think I just need a quick and dirty way. I got the data split up in Excel / NoteTab (ugh) and imported it to Access but the order of the fields was lost somehow!

    So thank you but the Bob solution is beyond my skill level. I don't even know where to put it, what to change and so on. I'm just not good at this yet. I'm trying though...

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Well, using Bob's original as a base, I have modified the function and called it SplitCodesTwo. I created a new table (TableForInsertNameHere) to accept your companyid and the text field alt_img TableForInsertNameHere.

    I have assumed your records in OneBigTable are companyId and alt_Img where alt_img contains the text you showed earlier.

    The new function is
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : SplitCodesTwo
    ' Author    : Jack
    ' Date      : 24/02/2012
    ' Purpose   : FROM: https://www.accessforums.net/showthread.php/14370-Access-2003-Split-Data-into-an-unknown-number-of-fields
    'original from Bob Larson
    '---------------------------------------------------------------------------------------
    '
    Function SplitCodesTwo()
    Dim varSplit()  As String  'array to hold result of the Split function
    Dim strSQL As String
    Dim rst As DAO.Recordset
    Dim db As DAO.Database
    Dim lngCount As Long
     
       On Error GoTo SplitCodesTwo_Error
    
    Set db = CurrentDb
    
    'adjusted to deal with your table and field names
    Set rst = db.OpenRecordset("Select [CompanyID], [alt_img] FROM OneBigTable")
     
    With rst
     
    Do Until .EOF
       varSplit = Split(![alt_img], "; ")  'Split at the "; "
       For lngCount = 0 To UBound(varSplit)
    
    'this new table was defined separately before running this function
    ' each array element becomes a new record in the new table
    'the companyid is from the original table
          strSQL = "INSERT INTO TableForInsertNameHere ( CompanyID, Alt_img) " & _
                       "VALUES (" & !CompanyID & ",'" & varSplit(lngCount) & "');"
                       Debug.Print strSQL 'Print the rendered sql before executing
          CurrentDb.Execute strSQL, dbFailOnError
       Next
       .MoveNext
     
    Loop
     
    End With
     
    rst.Close
    Set rst = Nothing
    
       On Error GoTo 0
       Exit Function
    
    SplitCodesTwo_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure SplitCodesTwo "
    End Function
    This was the only record in OneBigTable
    companyid alt_img
    1 AAG70260G05_2_1.JPG; AAG70260G05_3_1.JPG; AAG70260G05_5_1.JPG; AAG70260G05_6_1.JPG; AAG70260G05_7_1.JPG; AAG70260G05_8_1.JPG; AAG70260G05_4_1.JPG


    This is the output into the new table
    companyid alt_img
    1 AAG70260G05_2_1.JPG
    1 AAG70260G05_3_1.JPG
    1 AAG70260G05_5_1.JPG
    1 AAG70260G05_6_1.JPG
    1 AAG70260G05_7_1.JPG
    1 AAG70260G05_8_1.JPG
    1 AAG70260G05_4_1.JPG
    Last edited by orange; 02-25-2012 at 09:32 AM. Reason: added more comments to the function code

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

Similar Threads

  1. please help: similar field in a table
    By hadie in forum Queries
    Replies: 3
    Last Post: 12-30-2011, 08:26 AM
  2. Replies: 13
    Last Post: 06-20-2011, 12:18 PM
  3. Split Numeric Data out of Irregular Text Field
    By nathanielban in forum Queries
    Replies: 6
    Last Post: 12-21-2010, 10:30 AM
  4. similar data rows trouble
    By andyf80 in forum Database Design
    Replies: 3
    Last Post: 06-11-2010, 10:06 AM
  5. Split text field into two text fields
    By Grant in forum Access
    Replies: 6
    Last Post: 01-31-2008, 05: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