Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split a field in a form

Status
Not open for further replies.

simaq7

Technical User
Nov 18, 2010
1
0
0
US
How would I split a field first a name into two seperate fields?

Example Dow,John
Split into two fields

Last Nmae = Dow
First Name = John
 
Assuming you have told us the truth, the whole truth, and nothing but the truth:
Code:
=Left([Example],Instr([Example],",")-1)
=Mid([Example],Instr([Example],",")+1)


Duane
Hook'D on Access
MS Access MVP
 
You can also use the Split function which will give you an array
Geoff:

While that is true, it only works in VBA as the SPLIT function is not available directly in queries. The user would need to set up a public function to use the SPLIT function and return the applicable array item.

So, Duane's method is simpler. But I have used your suggestion in the past myself.

Bob Larson
Free Access Tutorials and Samples:
 
I also have used the Split() function in a couple user-defined functions that can be used in queries and control sources (as well as code).
Code:
Public Function ParseAttrib(strText As String, _
        strAttrib As String, _
        strDelimiter As String)
[green]    '---------------------------------------------------------
    ' Procedure : ParseAttrib
    ' Author    : Duane Hookom
    ' Date      : 10/1/2010
    ' Purpose   : Pull a single value from a multi-value string
    '             the strText must use = to match an attribute with its value
    '
    ' Samples
    ' --------------------------------------------------------
    ' ParseAttrib("color=red;Age=50;Dog=False","Color",";")=red
    ' ParseAttrib("color=red;Age=50;Dog=False","dog",";")=False
    '---------------------------------------------------------            [/green]
    Dim arText() As String
    Dim intI As Integer
    On Error GoTo ParseAttrib_Error

    arText() = Split(strText, strDelimiter)
    For intI = 0 To UBound(arText)
        If Split(arText(intI), "=")(0) = strAttrib Then
            ParseAttrib = Split(arText(intI), "=")(1)
        End If
    Next

    On Error GoTo 0
    Exit Function

ParseAttrib_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParseAttrib of Module basParseText"

End Function

Public Function ParseText(pstrText As String, intElement As Integer, _
        pstrDelimiter As String) As String
    Dim arText() As String
   On Error GoTo ParseText_Error

    arText() = Split(pstrText, pstrDelimiter)
    ParseText = arText(intElement - 1)
    
ExitParseText:
   On Error GoTo 0
   Exit Function

ParseText_Error:
    Select Case Err
        Case 9 [green]'subscript out of range
            'don't do anything[/green]
        Case Else
            MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParseText of Module basParseText"
    End Select
    Resume ExitParseText
End Function

Duane
Hook'D on Access
MS Access MVP
 
What if they have middle name or hyphonated surname? what about names such as O'toole?
have these been taken into account?


Ian Mayor (UK)
Program Error
9 times out of 10 I know what I'm talking about. This must be my tenth reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top