Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35

    The dreaded split

    I have a spreadsheet that is imported into a database with a 'Name' field. I need to split it because it is always Last,First,I and I need the First and Last name to be in their own columns....




    I've tried using the queries for Trim(Mid([employee],InStr([employee],",")+1,InStrRev([employee] & " "," ")-InStr([employee],","))) but with no luck on getting it to update the current table...

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    this code will break up the name and put in columns. (in Excel)
    it starts in H, and puts in I & J
    adjust this code so it will post the Last ,and First into the correct column:
    ActiveCell.Offset(0, 1).Formula = vFirst
    ActiveCell.Offset(0, 2).Formula = vLast


    Code:
    Public Sub SplitName()
    Dim i As Integer, c As Integer
    Dim vWord, vLast, vFirst, vMI, vRem
    
    c = 2
    Range("H2").Select
    While ActiveCell.Value <> ""
            'If Len(ActiveCell.Value) > 2 And LCase(ActiveCell.Value) <> "patron" Then
                
                vMI = ""
                vWord = Trim(ActiveCell.Text)
                i = InStrRev(vWord, " ")
                vLast = Trim(Mid(vWord, i + 1))
                vRem = vLast
                i = InStr(vWord, " ")
                vFirst = Left(vWord, i - 1)
                
    '            If InStr(vRem, " ") > 0 Then
    '               i = InStr(vRem, " ")
    '               vMI = Left(vRem, i - 1)
    '               vLast = Trim(Mid(vRem, i + 1))
    '            End If
                
                ActiveCell.Offset(0, 1).Formula = vFirst
                ActiveCell.Offset(0, 2).Formula = vLast
                'ActiveCell.Offset(0, 9).Formula = vMI
            
            'End If
            'c = c + 1
            ActiveCell.Offset(1, 0).Select   'next rows
    Wend
    
    End Sub

  3. #3
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    I need for access and not excel

  4. #4
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Assuming data in the format Last, First with a space after the comma

    Last: Left(Employee, InStr(Employee, ",") -1)

    First: Mid(Employee, InStr(Employee, ",") +2)

    Use these in an update query

    OR use the Split function in a VBA function - it can't be used in a query
    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!

  5. #5
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    I got the general update working if I was to self split the names into their own locations (First and Last columns). I'm unsure how to do this when just updating the current table.

  6. #6
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Import or link to your spreadsheet
    Have a separate table for your final result with separate columns.
    If records already exist, use an update query where the update to line has the expressions I gave before.
    If it's new data, use an append query with those expressions used to append to the last and first name fields.

    If it's a mixture of both, you can possibly combine into an 'UPEND' query.
    This updates existing records and appends new records in one query.
    To do s you use an update query but change it from inner join to outer join
    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!

  7. #7
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Well I got the update query to break the last and first name, but now it leaves a space and the middle initial in the right of the First name (ex. John I).

    Is there any way to just delete the space and the initial?

  8. #8
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Assuming not everybody has a space and initial, you could create a function in a standard module

    EDIT: 3 corrections in RED

    Code:
    Function GetFirstName(strText As String)
    strText = Mid(strText, InStr(strText, ",") + 2)
    If InStr(strtext, " ") > 0 Then
        GetFirstName = Left(strText, InStr(strText, " ") - 1)
    Else
        GetFirstName = strText
    End If
    End Function
    Now in your update query, use GetFirstName([Employee]) in the Update To line
    Last edited by isladogs; 07-31-2018 at 01:17 PM.
    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!

  9. #9
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Quote Originally Posted by ridders52 View Post
    Assuming not everybody has a space and initial, you could create a function in a standard module

    Code:
    Function GetFirstName(strText As String)
    strText = Mid(strText, InStr(strText, ",") + 2)
    If InStr(srtext, " ") > 0 Then
        GetFirstName = Left(strText, InStr(strText, " ") - 1)
    Else
        GetFirstName = strText
    End If
    End Function
    Now in your update query, use GetFirstName(Employee) in the Update To line
    Says undefined function. I put it in a standard module.

  10. #10
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Apologies: 3 corrections in RED

    Code:
    Function GetFirstName(strText As String)
    strText = Mid(strText, InStr(strText, ",") + 2)
    If InStr(strtext, " ") > 0 Then
        GetFirstName = Left(strText, InStr(strText, " ") - 1)
    Else
        GetFirstName = strText
    End If
    End Function
    Now in your update query, use GetFirstName([Employee]) in the Update To line[/QUOTE]
    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!

  11. #11
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Same outcome :/

  12. #12
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    I tested it this time which is why I made the corrections.
    It definitely works for me. See attached
    Attached Files Attached Files
    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!

  13. #13
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    That worked, thanks!

    Strange, I had to rename the module back to 'Module1'.

  14. #14
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    It doesn't matter what the module is called except the module and function cannot have the same name
    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!

  15. #15
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Then that explains that...thanks for the help!

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

Similar Threads

  1. Replies: 3
    Last Post: 08-01-2017, 01:14 PM
  2. Replies: 3
    Last Post: 11-03-2015, 11:19 PM
  3. to split or not to split my db?
    By RLehrbass in forum Access
    Replies: 6
    Last Post: 09-17-2015, 01:54 PM
  4. Get split form behaviours without using a split form
    By SyntaxSocialist in forum Queries
    Replies: 2
    Last Post: 04-12-2013, 11:56 AM
  5. When to split
    By Mclaren in forum Database Design
    Replies: 4
    Last Post: 07-07-2010, 07:25 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