1

按姓氏搜索时,我需要搜索以在数据库中查找“模糊匹配”。我已经实现了以下功能来启用 SoundEx 功能,这没有问题。

在构建 SQL 以搜索动态调用 SoundEx 函数的数据库时,我遇到了问题。我知道可以在 SQL 语句中调用 VBA 函数,但它似乎不能正常工作。

Private Sub cmdSearch_Click()

Dim LSQL  As String
Dim LSearchString As String

If Len(txtSearchString) = 0 Or IsNull(txtSearchString) = True Then
    MsgBox "You must enter a search string."

Else
    LSearchString = txtSearchString
    LSQL = "SELECT * FROM TVictim "
    LSQL = LSQL & "WHERE Soundex([Victim Surname]) = " & Soundex(LSearchString) & ";"

    frmVictim.Form.RecordSource = LSQL


    lblTitle.Caption = "Matching Victim Details:  Filtered by '" & LSearchString & "'"
    txtSearchString = ""

    MsgBox "Results have been filtered.  All Victim Names containing " & LSearchString & "."

End If End Sub

当我在表单上输入一个字符串并单击按钮时,我已经逐步完成,并且在它构建 SQL 时,它会在搜索框中弹出一个命令窗口,其中包含文本的 SoundEx 输出,以及另一个数据框入口。

一直在摆弄这个,似乎找不到有帮助的例子。

4

1 回答 1

1

我在 Access 2003 中使用 Allen Browne 的 Soundex 函数:Soundex - 模糊匹配

它以字符串形式返回 Soundex 值。如果您使用的 Soundex 函数也返回一个字符串,请用引号将Soundex(LSearchString)括起来,以便数据库引擎将其识别为字符串值,而不是缺失参数的名称。

LSQL = LSQL & "WHERE Soundex([Victim Surname]) = '" & Soundex(LSearchString) & "';"
于 2011-04-29T04:58:27.977 回答