Versions : 2000 et supérieures
Ce code permet de rechercher des occurences proches phonétiquement d'un critère. Pour cela, la méthode utilisée est celle de l'algorithme Soundex. Vous en trouverez les origines ici.
Je vous propose donc une traduction de cet algorithme en VBA :
Option Compare Database
Option Explicit
Public Function Prepare(Mot As String) As String
Dim I As Integer
Dim Caractere
Mot = Trim$(UCase$(Mot))
For I = 1 To Len(Mot)
Caractere = Mid(Mot, I, 1)
Select Case Asc(Caractere)
Case 192, 194, 196
Prepare = Prepare & "A"
Case 199
Prepare = Prepare & "S"
Case 200 To 203
Prepare = Prepare & "E"
Case 204, 206, 207
Prepare = Prepare & "I"
Case 212, 214
Prepare = Prepare & "O"
Case 217, 219, 220
Prepare = Prepare & "U"
Case Else
If Caractere <> " " & Caractere <> "-" Then _
Prepare = Prepare & Caractere
End Select
Next I
End Function
Public Function Soundex(Mot As String) As String
Dim TLettre() As Variant
Dim I As Integer
Dim Lettre As String
Dim Tmp As String
TLettre = Array("", "1", "2", "3", "", "1", "2", "", "", "2", _
"2", "4", "5", "5", "", "1", "2", "6", "2", "3", _
"", "1", "", "2", "", "2")
Mot = Prepare(Mot)
If Len(Mot) = 0 Then
Soundex = "0000"
Else
If Len(Mot) = 1 Then
Soundex = Mot & "000"
Else
If Left(Mot, 1) = "H" Then Mot = Mid(Mot, 2)
Soundex = Left(Mot, 1)
For I = 2 To Len(Mot)
Lettre = Mid(Mot, I, 1)
If Lettre >= "A" And Lettre <= "Z" Then
Tmp = TLettre(Asc(Lettre) - Asc("A"))
If Tmp <> Right(Soundex, 1) Then Soundex = Soundex & Tmp
End If
Next I
End If
End If
If Len(Soundex) >= 4 Then
Soundex = Left(Soundex, 4)
Else
For I = Len(Soundex) To 3
Soundex = Soundex & "0"
Next I
End If
End Function
Ainsi, Soundex("Martin") et Soundex("Martan") renvoient la même valeur.
Vous trouverez un exemple d'utilisation complet dans des requêtes SQL en télechargeant le fichier Zip.
|