Error on parse name

T

TimH

I have the code for parse name(giving credit to original coder) and as I
study the code I get an error that FirstName is zero length even though it is
fully entered such as LastName, FirsName

If there is a middle name the error does not show up

Here is the code for the parse name :

Function ParseName(strOutSal As String, strOutFirst As String, _
strOutMiddle As String, strOutFinal As String, _
strOutSuffix As String, ByVal strInName As Variant) As
Integer
'
' Purpose: Parse a full name into its constituent parts
' Arguments: strOutSal Salutation Output
' strOutFirst First name Output
' strOutMiddle Middle name Output
' strOutFinal Last name Output
' strOutSuffix Suffix Output
' strInName Name to parse
' Returns: Hex flag value, determining which components were filled in
' Author: David Sussman
' Date: 8 June 1997

Dim strTemp() As String ' Temporary work string
Dim intCount As Integer ' No. of elements in above string
Dim intSpacePos As Integer ' Position in string of space
Dim intLoop As Integer ' Loop index
Dim intParsename As Integer ' Return value
Dim intMainCount As Integer ' No. of elements - prefix & suffix

ParseName = 0
ReDim strTemp(0) As String

' exit if name is blank, or add trailing space
' to ensure the InStr does not fail
If IsBlank(strInName) Then Exit Function
strInName = Trim$(strInName) & " "

' break the string into an array
intSpacePos = InStr(strInName, " ")
Do Until intSpacePos = 0
ReDim Preserve strTemp(UBound(strTemp) + 1)
strTemp(UBound(strTemp)) = Trim$(Left$(strInName, intSpacePos - 1))
strInName = LTrim$(Right$(strInName, Len(strInName) - intSpacePos))
intSpacePos = InStr(strInName, " ")
Loop

intCount = UBound(strTemp)
intMainCount = intCount

' is there a prefix
If InStr(csPREFIX_LIST, "." & UCase$(strTemp(1)) & ".") Then
intParsename = intParsename Or csNAME_SAL
strOutSal = strTemp(1)
intMainCount = intMainCount - 1
End If

' is there a suffix
If InStr(csSUFFIX_LIST, "." & UCase$(strTemp(intCount)) & ".") Then
intParsename = intParsename Or csNAME_SUFFIX
strOutSuffix = strTemp(intCount)
intMainCount = intMainCount - 1
End If

' How many elements are left
Select Case intMainCount
Case 0
Case 1
' one element left - must be the surname
If (intParsename And csNAME_SAL) Then
strOutFinal = strTemp(2)
Else
strOutFinal = strTemp(1)
End If
intParsename = intParsename Or csNAME_LAST
Case 2
' two elements left - first surname and name

If (intParsename And csNAME_SUFFIX) Then
strOutFinal = strTemp(intCount - 1)
Else
strOutFinal = strTemp(intCount)
End If
intParsename = intParsename Or csNAME_LAST

If (intParsename And csNAME_SAL) Then
strOutFirst = strTemp(2)
Else
strOutFirst = strTemp(1)
End If
intParsename = intParsename Or csNAME_FIRST
Case Else

' three or more elements - first name, surname
' and one or more middle names
If (intParsename And csNAME_SAL) Then
strOutFirst = strTemp(2)
Else
strOutFirst = strTemp(1)
End If
intParsename = intParsename Or csNAME_LAST

If (intParsename And csNAME_SUFFIX) Then
strOutFinal = strTemp(intCount - 1)
Else
strOutFinal = strTemp(intCount)
End If
intParsename = intParsename Or csNAME_FIRST

' middle names
For intLoop = 2 To intMainCount - 1
If (intParsename And csNAME_SAL) Then
strOutMiddle = Trim$(strOutMiddle) & " " & strTemp(intLoop +
1)
Else
strOutMiddle = Trim$(strOutMiddle) & " " & strTemp(intLoop)
End If
intParsename = intParsename Or csNAME_MID
Next
strOutMiddle = Trim$(strOutMiddle)
End Select

ParseName = intParsename

End Function
 
M

Margaret Bartley

Step through the code. Using the Command window and the Locals window, you
should be able to identify where a variable has an unexpected value.

At that point, if you try to articulate a specific question to the group,
you'll probably answer it yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA Export to PDF 0
VBA Coding Help for Beginner 0
CheckIBAN 5
export vba not working 0
Export to Mutiple Spreadsheets 1
return without gosub error 0
return without gosub error 0
Match style 0

Top