MS Access查找并突出显示与长文本字段中的任何表列列表值匹配的多个子字符串

问题描述 投票:0回答:1

我是一个初学者,试图找出一个策略。

样本数据表:成分领域:FormulaIngredients字段内容(长文本富文本):大头菜,韭菜,胡萝卜,小麦,面粉,黄油,糖,鸡蛋,牛奶,花生酱,石磨玉米粉,全麦燕麦,卷心菜,姜黄,丁香,香料,天然牛肉味,碎牛肉。

表:RestrictedTable列,包含100个要比较的值:RestrictedItem示例列值:牛奶烘烤勺胡萝卜芥末蒸汽

期望的结果:想要突出显示/更改FormulaIngredients字段中与表列中的任何~100个值匹配的匹配字体:RestrictedItem。想象一下斜体字是红色的。

大头菜,韭菜,胡萝卜,小麦,面粉,黄油,糖,鸡蛋,牛奶,花生酱,石磨玉米粉,全麦燕麦,白菜,姜黄,丁香,香料,天然牛肉味,碎牛肉,芥末。

或者,将FormulaIngredients的内容复制并替换为新字段,该字段将红色应用于与表列匹配的单词:RestrictedItem。

我探索过......

InStr问题:我不想将一串信息传递给表单/报表,也不关心找到子字符串的位置,我想找到所有这些,无论如何,重复都可以。

Dim strTemp, strTempEnd As String
Dim strSearch As String

strSearch = Me.OpenArgs

If InStr(1, Me.Text0, strSearch) <> 0 Then
    strTemp = Left(Me.Text0, InStr(1, Me.Text0, strSearch) - 1)
    strTempEnd = Mid(Me.Text0, Len(strTemp) + Len(strSearch) + 1)
    strTemp = strTemp & "<font color=red>" & strSearch & "</font>"
    strTemp = strTemp & strTempEnd

    Me.Text0 = strTemp
End If

HTML问题:此解决方案比较2列数据。我想将一个字段与一个值表进行比较,并在该一个长文本字段中找到多个匹配项。

    Public Function MyCompare(c1t As Variant, c2t As Variant)

  Dim strResult    As String
  Dim s        As String
  Dim i        As Integer
  Dim bolSame     As Boolean

  If IsNull(c1t) Or IsNull(c2t) Then Exit Function

  bolSame = True

  For i = 1 To Len(c2t)
   s = Mid(c2t, i, 1)
   If Mid(c1t, i, 1) = s Then
    If bolSame = False Then
      bolSame = True
      s = "</strong></font>" & s
     End If
   Else
     If bolSame = True Then
      bolSame = False
      s = "<font color=red><strong>" & s
     End If
   End If
   strResult = strResult & s
  Next

  If bolSame = False Then
   strResult = strResult & "</strong></font>"
  End If
  MyCompare = strResult

End Function

VBA问题:我必须在我的表格中输入我想要在我的长文本表单字段中进行比较/搜索的所有100个关键字,并且REPLACE是区分大小写的搜索。有没有办法比较价值表?

Me.{ControlName}.Value = Replace(Me.{ControlName}.Value _
                               , "red", "<font color=red>red</font>")
html ms-access access-vba richtextbox
1个回答
0
投票

建议VBA程序:

  1. 限制表的开放记录集
  2. 循环遍历记录集并在Ingredients表上运行UPDATE操作SQL

例:

Dim db As DAO.Database  
Dim rs As DAO.Recordset  
Set db = CurrentDb  
Set rs = db.OpenRecordset("SELECT * FROM RestrictedTable")  
Do While Not rs.EOF  
    db.Execute "UPDATE Ingredients SET FormulaIngredients = Replace([FormulaIngredients], '" & rs!RestrictedItem & "', '<font color=red>" & rs!RestrictedItem & "</font>')"  
    rs.MoveNext  
Loop

我用静态参数进行了快速测试,但它确实有效。

CurrentDb.Execute "UPDATE Ingredients SET FormulaIngredients = Replace([FormulaIngedients], 'carrots', '<font color=red>carrots</font>')"

如果公式文本有CARROTScarrotsCArRoTs无关紧要 - 所有都将匹配并被carrots取代。但是,如果公式文本有carrot,则不匹配。

部分匹配可能不是问题,但如果您想确保只更改整个单词,请在搜索之前和之后使用空格并替换字符串:' carrots '。除非你想让milk中的buttermilk得到突出显示。

如果您不想更改原始数据,请先复制到另一个字段,然后在UPDATE语句中使用该其他字段。这可以使用Do While块之前的UPDATE语句来完成。

db.Execute "UPDATE Ingredients SET FormulaHighlight=FormulaIngredients"

© www.soinside.com 2019 - 2024. All rights reserved.