HM said:
Hello all,
Does anyone know in VB have Sound index command?
Thank you in advange.
HM
If you're referring to the Soundex algorithm, it's not built in. I got
the code below from somewhere, I don't know where. Watch out for lines
wrapped by the newsreader:
'-------- start of code --------
Function Soundex(Name As String) As String
' Implements SOUNDEX algorithm, reasonably efficiently.
' Uses Array lookup to get soundex code digits and a
' relatively fast loop to scan the name.
' Returns null string if passed string contains numeric characters
' or non-numeric characters other than ', -, or <Space>, which are
' ignored (i.e. treated as if the characters on either side of them
' are directly adjacent to each other).
Static CodeLookup As Variant
Static InitDone As Boolean
Dim SoundTemp As String
Dim NameTemp As String
Dim ThisVal As Integer
Dim PrevVal As Integer
Dim ThisChar As Integer
If Not InitDone Then
CodeLookup = Array(0, 1, 2, 3, 0, 1, 2, -1, 0, 2, 2, 4, 5, 5, 0, 1,
2, 6, 2, 3, 0, 1, -1, 2, 0, 2)
InitDone = True
End If ' only need to do this once
NameTemp = UCase(RTrim$(LTrim$(Name)))
Soundex = vbNullString
If Len(NameTemp) = 0 Then Exit Function
ThisChar = Asc(NameTemp)
If IsCharAlpha(ThisChar) = 0 Then Exit Function ' first character of
name must be alpha
SoundTemp = Mid$(NameTemp, 1, 1)
NameTemp = Mid$(NameTemp, 2)
PrevVal = CodeLookup(ThisChar - 64)
While Len(NameTemp) > 0 And Len(SoundTemp) < 4
ThisChar = Asc(NameTemp)
If IsCharAlpha(ThisChar) Then
ThisVal = CodeLookup(ThisChar - 64)
ElseIf ThisChar = 32 Or ThisChar = 39 Or ThisChar = 45 Then
ThisVal = -1 ' included hyphens, apostrophes, and spaces are
treated like H or W
Else
Exit Function ' invalid character in name
End If
If ThisVal = PrevVal Or ThisVal = 0 Then
' do nothing
ElseIf ThisVal = -1 Then
ThisVal = PrevVal ' H, W, and punctuation are totally "silent"
Else
SoundTemp = SoundTemp & ThisVal
End If
PrevVal = ThisVal
NameTemp = Mid$(NameTemp, 2)
Wend
While Len(SoundTemp) < 4
SoundTemp = SoundTemp & "0"
Wend
Soundex = SoundTemp
End Function
'-------- end of code --------