Make sure you test the following code in a COPY of your dB!!!
'rule "B-2" - if there is no number following the "/" and
' there is no "W" following the "/"
'use rule "A"
Not sure about 'rule "B-2"..... There were no examples of a string with a number or "W" after the slash in post#11.
The function works for every example in post #11:
Code:
Public Function fGetFirstChars_Nums_w(pString As Variant) As String
Dim tmp As String
Dim strRemStr As String
Dim strPrevChar As String
Dim bytChrLoc As Byte
Dim cntr As Integer
Dim HasSlash As Boolean
Dim HasHyphen As Boolean
Dim HasW As Boolean
Dim HasSlash As Boolean
Dim HasHyphen As Boolean
Dim HasW As Boolean
'do not allow NULLS
If IsNull(pString) Then
'change the "??" to whatever you want
fGetFirstChars_Nums_w = "??"
' fGetFirstChars_Nums_w = vbNullString '<<-- alternative to "??"
Exit Function
End If
'check for Slash, W and Hyphen
HasSlash = InStr(pString, "/")
HasW = InStr(pString, "W")
HasHyphen = InStr(pString, "-")
'Handle String with slash
If HasSlash Then
bytChrLoc = InStr(1, pString, "/")
'get everythin before slash
tmp = Left(pString, bytChrLoc - 1)
strPrevChar = Right(tmp, 1)
'is last char numeric?
If IsNumeric(strPrevChar) Then
strRemStr = tmp
Else
If HasW Then
'remove everthing after the "W"
For cntr = Len(tmp) To 1 Step -1
strPrevChar = Mid(tmp, cntr, 1)
If strPrevChar = "W" Then
strRemStr = Left(tmp, cntr)
HasW = False
Exit For
End If
Next cntr
Else
'remove everthing after the last number if not "W"
For cntr = Len(tmp) To 1 Step -1
strPrevChar = Mid(tmp, cntr, 1)
If IsNumeric(strPrevChar) Then
strRemStr = Left(tmp, cntr)
Exit For
End If
Next cntr
End If 'If HasW
End If 'If IsNumeric(strPrevChar)
End If 'If HasSlash
'Handle String with "W"
If HasW Then
'remove everthing after the "W"
For cntr = Len(pString) To 1 Step -1
strPrevChar = Mid(pString, cntr, 1)
If strPrevChar = "W" Then
strRemStr = Left(pString, cntr)
Exit For
End If
Next cntr
End If
'Handle String with Hyphen
If HasHyphen Then
'no slash and no "W" so just return pString
strRemStr = pString
End If
'Handle String with no slash, no "W" and no hyphen
' check if last char of pString is numeric
If Not HasSlash And Not HasW And Not HasHyphen Then
strPrevChar = Right(pString, 1)
'is last char numeric?
If IsNumeric(strPrevChar) Then
strRemStr = pString
Else
'not numeric, strip off letters
For cntr = Len(pString) To 1 Step -1
strPrevChar = Mid(pString, cntr, 1)
If IsNumeric(strPrevChar) Then
strRemStr = Left(pString, cntr)
Exit For
End If
Next cntr
End If
End If
'return value
fGetFirstChars_Nums_w = strRemStr
End Function
Remember what I said about using a copy of the dB...