I'm worried I also may not be understanding how I'm supposed to interact with the AB code. I didn't change anything in his, so it's:
Code:
Function ParseWord(varPhrase As Variant, ByVal iWordNum As Integer, Optional strDelimiter As String = " ", _
Optional bRemoveLeadingDelimiters As Boolean, Optional bIgnoreDoubleDelimiters As Boolean) As Variant
On Error GoTo Err_Handler
'Purpose: Return the iWordNum-th word from a phrase.
'Return: The word, or Null if not found.
'Arguments: varPhrase = the phrase to search.
' iWordNum = 1 for first word, 2 for second, ...
' Negative values for words form the right: -1 = last word; -2 = second last word, ...
' (Entire phrase returned if iWordNum is zero.)
' strDelimiter = the separator between words. Defaults to a space.
' bRemoveLeadingDelimiters: If True, leading delimiters are stripped.
' Otherwise the first word is returned as null.
' bIgnoreDoubleDelimiters: If true, double-spaces are treated as one space.
' Otherwise the word between spaces is returned as null.
'Author: Allen Browne. http://allenbrowne.com. June 2006.
Dim varArray As Variant 'The phrase is parsed into a variant array.
Dim strPhrase As String 'varPhrase converted to a string.
Dim strResult As String 'The result to be returned.
Dim lngLen As Long 'Length of the string.
Dim lngLenDelimiter As Long 'Length of the delimiter.
Dim bCancel As Boolean 'Flag to cancel this operation.
'*************************************
'Validate the arguments
'*************************************
'Cancel if the phrase (a variant) is error, null, or a zero-length string.
If IsError(varPhrase) Then
bCancel = True
Else
strPhrase = Nz(varPhrase, vbNullString)
If strPhrase = vbNullString Then
bCancel = True
End If
End If
'If word number is zero, return the whole thing and quit processing.
If iWordNum = 0 And Not bCancel Then
strResult = strPhrase
bCancel = True
End If
'Delimiter cannot be zero-length.
If Not bCancel Then
lngLenDelimiter = Len(strDelimiter)
If lngLenDelimiter = 0& Then
bCancel = True
End If
End If
'*************************************
'Process the string
'*************************************
If Not bCancel Then
strPhrase = varPhrase
'Remove leading delimiters?
If bRemoveLeadingDelimiters Then
strPhrase = Nz(varPhrase, vbNullString)
Do While Left$(strPhrase, lngLenDelimiter) = strDelimiter
strPhrase = Mid(strPhrase, lngLenDelimiter + 1&)
Loop
End If
'Ignore doubled-up delimiters?
If bIgnoreDoubleDelimiters Then
Do
lngLen = Len(strPhrase)
strPhrase = Replace(strPhrase, strDelimiter & strDelimiter, strDelimiter)
Loop Until Len(strPhrase) = lngLen
End If
'Cancel if there's no phrase left to work with
If Len(strPhrase) = 0& Then
bCancel = True
End If
End If
'*************************************
'Parse the word from the string.
'*************************************
If Not bCancel Then
varArray = Split(strPhrase, strDelimiter)
If UBound(varArray) >= 0 Then
If iWordNum > 0 Then 'Positive: count words from the left.
iWordNum = iWordNum - 1 'Adjust for zero-based array.
If iWordNum <= UBound(varArray) Then
strResult = varArray(iWordNum)
End If
Else 'Negative: count words from the right.
iWordNum = UBound(varArray) + iWordNum + 1
If iWordNum >= 0 Then
strResult = varArray(iWordNum)
End If
End If
End If
End If
'*************************************
'Return the result, or a null if it is a zero-length string.
'*************************************
If strResult <> vbNullString Then
ParseWord = strResult
Else
ParseWord = Null
End If
Exit_Handler:
Exit Function
Err_Handler:
'Call LogError(Err.Number, Err.Description, "ParseWord()")
Resume Exit_Handler
End Function
And then I tried programming a button. I'd initially thought the AB code was meant to be a Standard Module, but was getting errors until I moved it in with the rest of the code on the form. This code has a couple of different methods I tried; this is just what it is as of right now:
Code:
Private Sub btnID_Click()
'Generate Client ID based on name user enters.
'Declare variables.
Dim strName As String 'Client Name user entered.
Dim strOne As String 'First word in client name field.
Dim strTwo As String 'Second word in client name field.
Dim strFHalf As String 'First half of ID.
Dim strSHalf As String 'Secoond half of ID.
Dim strID As String 'Client ID (minus numbers)
'Store user-entered client name in variable.
' strName = Me.ctlEntPrimName.Value
' Debug.Print
'Parse first word from client name, convert to upper case.
strOne = ParseWord([ctlEntPrimName], 1, , True, True)
Debug.Print
'Parse second word from client name, covnert to upper case.
strTwo = ParseWord([ctlEntPrimName], 1, , True, True)
Debug.Print
'If first word longer than 4 letters, trim to 4.
If Len(strOne) > 4 Then
strFHalf = Left$(strOne, 4)
Else: strFHalf = strOne
End If
Debug.Print
'If second word longer than 4 letters, trim to 4.
If Len(strTwo) > 4 Then
strSHalf = Left$(strTwo, 4)
Else: strSHalf = strTwo
End If
Debug.Print
'Combine the two halves.
strID = strFHalf & strSHalf
Debug.Print
'Me.ctlEntID = Left([ctlEntPrimName], InStr(InStr(1, [ctlEntPrimName]) + 1, [ctlEntPrimName]) - 1)
'Debug.Print
End Sub
Our clients are lending institutions, and I've never seen one start with "A". "The" is fine, for my purposes; in my code, I tried to accomodate that by only trimming to four letters if the words were longer than 4.
These IDs are very important. They're the Primary Key for the table, and identify the client and everything (accounts, policies, contacts, etc) linked to the client.