Hi there, the code below returns the Soundex value for a string.
For example: both "J. Bloggs" and "Joe Bloggs" have a Soundex value J14200, (but "Bloggs, J" gives "B42000" so will need to be handled before passing into the Soundex routine).
Function ahtSoundex(ByVal varSurName As Variant) As Variant
' Purpose:
' Takes a surname string and returns a 4-digit
' code representing the Russell Soundex code.
'
' From Microsoft Access 95 How-To
' by Getz and Litwin. (Waite Group)
' Copyright 1995. All Rights Reserved.
'
' In:
' varSurName: A surname (last name) as a variant
' Out:
' Return value: A 4-digit Soundex code as a variant
On Error GoTo ahtSoundexErr
Dim intLength As Integer
Dim intCharCount As Integer
Dim intSdxCount As Integer
Dim intSeparator As Integer
Dim intSdxCode As Integer
Dim intPrvCode As Integer
Dim varChar As Variant
Dim varSdx As Variant
Const ahtcSoundexLength = 6
' We add "" to take care of a passed Null
intLength = Len(varSurName & ""
If intLength > 0 Then
intSeparator = 0 'Keeps track of vowel separators
intPrvCode = 0 'The code of the previous char
intCharCount = 0 'Counts number of input chars
intSdxCount = 0 'Counts number of output chars
'Loop until the soundex code is of ahtcSoundexLength
'or we have run out of characters in the surname
Do Until (intSdxCount = ahtcSoundexLength Or intCharCount = intLength)
intCharCount = intCharCount + 1
varChar = Mid(varSurName, intCharCount, 1)
'Calculate the code for the current character
Select Case varChar
Case "B", "F", "P", "V"
intSdxCode = 1
Case "C", "G", "J", "K", "Q", "S", "X", "Z"
intSdxCode = 2
Case "D", "T"
intSdxCode = 3
Case "L"
intSdxCode = 4
Case "M", "N"
intSdxCode = 5
Case "R"
intSdxCode = 6
Case "A", "E", "I", "O", "U", "Y"
intSdxCode = -1
Case Else
intSdxCode = -2
End Select
'Special case the first character
If intCharCount = 1 Then
varSdx = UCase(varChar)
intSdxCount = intSdxCount + 1
intPrvCode = intSdxCode
intSeparator = 0
'If a significant constant and not a repeat
'without a separator then code this character
ElseIf intSdxCode > 0 And _
(intSdxCode <> intPrvCode Or intSeparator = 1) Then
varSdx = varSdx & intSdxCode
intSdxCount = intSdxCount + 1
intPrvCode = intSdxCode
intSeparator = 0
'If a vowel, this character is not coded,
'but it will act as a separator
ElseIf intSdxCode = -1 Then
intSeparator = 1
End If
Loop
'If the code is < ahtcSoundexLength chars long, then
'fill the rest of code with zeros
If intSdxCount < ahtcSoundexLength Then
varSdx = varSdx & String((ahtcSoundexLength - intSdxCount), "0"

End If
ahtSoundex = varSdx
Else
ahtSoundex = Null
End If
ahtSoundexDone:
On Error GoTo 0
Exit Function
ahtSoundexErr:
Select Case err
Case Else
MsgBox "Error#" & err & ": " & Error$, _
vbOKOnly + vbCritical, "ahtSoundex"
End Select
Resume ahtSoundexDone
End Function