使用 Access VBA 代码,如何使用 table 列替换评论框中的单词以搜索一组我想要替换的单词?

Using Access VBA code, how do I replace words in a comment box using table columns to search for a set of words I want replaced?

我想在访问中引用 table 来替换评论框中的单词。我会搜索第 1 列中的词并将它们替换为第 2 列中的词。我不确定如何正确命名列以将它们插入替换函数中。

这是我尝试使用的代码示例,

Private Sub Replace_Click()

Dim bullet As String
Dim output As String

bullet = commentBox.Value
commentBox.Value = Replace(bullet, [tbl_name].column_name, [tbl_name].column_name)

End Sub

要考虑的选项:

  1. 打开 table 的记录集,遍历记录并对每个值执行替换。如果值在字符串中,它将被替换,如果它不在字符串中,则什么也不会发生。
Sub SubAbb()
Dim rs As DAO.Recordset, sStr As String
Set rs = CurrentDb.OpenRecordset("SELECT Word, Abb FROM Words")
sStr = Me.commentBox
Do While Not rs.EOF
    sStr = Replace(sStr, rs!Word, rs!Abb)
    rs.MoveNext
Loop
Me.commentBox = sStr
End Sub
  1. 将字符串拆分为数组,遍历数组并在 table 上执行 DLookup。如果找到缩写,运行 替换字符串。但是,这假定字符串只有由单个 space 分隔的单词,没有标点符号或数字或日期,这会使代码复杂化。
Sub SubAbb()
Dim sStr As String, sAbb As String, sAry As Variant, x As Integer
sStr = Me.commentBox
sAry = Split(sStr, " ")
For x = 0 To UBound(sAry)
    sAbb = Nz(DLookup("Abb", "Words", "Word='" & sAry(x) & "'"), "")
    If sAbb <> "" Then sStr = Replace(sStr, sAry(x), sAbb)
Next
Me.commentBox = sStr
End Sub