简体   繁体   English

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

[英]MS Access Find & Highlight Multiple Substrings that Match Any Table Column List Value in Long Text Field

I'm a beginner trying to figure out a strategy. 我是一个尝试制定策略的初学者。

Sample Data Table: Ingredients Field: FormulaIngredients Field Contents (Long Text Rich Text): rutabaga, leeks, carrots, wheat, flour, butter, sugar, eggs, milk, peanut butter, stone ground corn meal, whole grain oats, cabbage, turmeric, cloves, spice, natural beef flavor, ground beef. 样本数据 表:成分字段:配方成分字段 内容(长文本,带丰富文本):大头菜,韭菜,胡萝卜,小麦,面粉,黄油,糖,鸡蛋,牛奶,花生酱,磨碎的玉米粉,全麦燕麦,白菜,姜黄,丁香,香料,天然牛肉风味,碎牛肉。

Table: RestrictedTable Column with 100 values to compare: RestrictedItem Example Column Values: milk bake spoon carrots mustard steam 表: 带有100个要比较的值的 RestrictedTable 列: RestrictedItem 示例列值:牛奶烤勺胡萝卜芥末蒸

Desired Result : Want to highlight/change font of matches within FormulaIngredients field that match to any of the ~100 values in a Table Column: RestrictedItem. 所需结果 :想要突出显示/更改FormulaIngredients字段中与表列:RestrictedItem中的〜100值中的任何一个匹配的匹配字体。 Imagine the italicized words are red. 假设斜体字是红色的。

rutabaga, leeks, carrots , wheat, flour, butter, sugar, eggs, milk , peanut butter, stone ground corn meal, whole grain oats, cabbage, turmeric, cloves, spice, natural beef flavor, ground beef, mustard . 大头菜,韭菜, 胡萝卜 ,小麦,面粉,黄油,糖,鸡蛋, 牛奶 ,花生酱,碎玉米粉,全谷物燕麦,白菜,姜黄,丁香,香料,天然牛肉味,牛肉末, 芥末

Or, copy & replace the content of FormulaIngredients to a new field which applies a red color to those words that match to Table Column: RestrictedItem. 或者,将FormulaIngredients的内容复制并替换到新字段,该字段将对与表列:RestrictedItem匹配的单词使用红色。

I've explored... 我探索过...

InStr Problem : I don't want to pass a string of info to a form/report, also do not care about the position the substring was found in, I want to find all of them, in any case & duplicates are ok. 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 Problem : This solution compares 2 columns of data. HTML 问题 :此解决方案比较2列数据。 I want to compare one field to a table of values & find multiple matches within that one Long Text field. 我想将一个字段与值表进行比较,并在一个Long Text字段中找到多个匹配项。

    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 Problem : I would have to type out all 100 keywords in my table that I want to compare/search in my Long Text form field & REPLACE is a case sensitive search. VBA 问题 :我必须在我的表中输入要比较/搜索的所有100个关键字,并且要在长文本表单字段中进行搜索,并且REPLACE是区分大小写的搜索。 Is there a way to compare to a table of values? 有没有一种方法可以与值表进行比较?

Me.{ControlName}.Value = Replace(Me.{ControlName}.Value _
                               , "red", "<font color=red>red</font>")

Suggest VBA procedure that: 建议VBA程序:

  1. opens recordset of RestrictedTable 打开RestrictedTable的记录集

  2. loops through recordset and runs an UPDATE action SQL on Ingredients table 遍历记录集并在成分表上运行UPDATE操作SQL

Example: 例:

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

I did a quick test with static parameter and it worked. 我用静态参数做了一个快速测试,它起作用了。

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

Doesn't matter if the formula text has CARROTS , carrots , CArRoTs - all will match and be replaced with carrots . 公式文本中是否包含CARROTScarrotsCArRoTs -全部都匹配并替换为carrots However, if formula text has carrot , it will not match. 但是,如果公式文本中有carrot ,它将不匹配。

Partial matches might not be a concern, but if you want to be sure to change only whole words, use space before and after the search and replace strings: ' carrots ' . 可能不存在部分匹配的问题,但是如果您要确保只更改整个单词,请在搜索前后使用空格并替换字符串: ' carrots ' Unless you want milk in buttermilk to get highlighted. 除非您想突出buttermilk milk

If you don't want to alter the original data, first copy to another field then use that other field in the UPDATE statement. 如果您不想更改原始数据,请首先复制到另一个字段,然后在UPDATE语句中使用该另一个字段。 This can be done with an UPDATE statement before the Do While block. 这可以通过“ Do While块之前的UPDATE语句来完成。

db.Execute "UPDATE Ingredients SET FormulaHighlight=FormulaIngredients"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM