搜索多个自由格式文本以
Search multiple free form texts to
我正在使用 Access 2010。我有一个数据库,用于记录流程中错误的自由格式文本。我想创建一个函数,用于在自由格式文本中搜索关键字并根据错误代码进行报告。虽然不确定如何去做:
前:
ID | 降序
1 |松散部分
2 |油漆污迹
3 |没有螺栓
与另一个table
中的关键字进行比较
关键字 | 错误代码
油漆 | 32
螺栓 | 25
部分 | 55
输出看起来像
ID | 错误代码
1 | 55
2 | 32
3 | 25
我的想法是我可以更新关键字 table 以包含新关键字和错误代码作为更多弹出窗口。
我给你一个可能的解决方案:
假设您的 table 名称为 tbDescriptions、tbKeywords、tbErrors。
tbErrors 的 ID 字段是自动编号的(如果不是,您只需在重建期间添加 ID 值)。
这是一个子程序,您可以使用它来重建错误 table。
请注意,我假设 tbErrors 可以在您每次需要更新时删除并重建。如果不是这样,您需要管理 tbErrors 中 Error 的存在,但这并不复杂。
Private Sub RebuildErrorTable()
Dim rsKeyWords As dao.Recordset, rsErrors As dao.Recordset
Dim strKeyword As String
Dim strSELECT As String
Dim strWhereCond As String
Dim intNumOccurrences As Integer
'
' Delete all records from tbErrors
'
DoCmd.SetWarnings False
DoCmd.RunSQL = "DELETE * FROM tbErrors"
DoCmd.SetWarnings True
'
' Rebuild table tbErrors
'
strSELECT = "SELECT * FROM tbKeyWords"
Set rsKeyWords = CurrentDb.OpenRecordset(strSELECT, dbOpenDynaset) ' Open keywords table
strSELECT = "SELECT * FROM tbErrors"
Set rsErrors = CurrentDb.OpenRecordset(strSELECT, dbOpenDynaset) ' Open Errors table for rebuilding
'
' Scan all keywords
'
Do While Not rsKeyWords.EOF
strKeyword = rsKeyWords!keywords ' Current keyword
strWhereCond = "Desc LIKE '*" + strKeyword + "*'" ' Build search condition
intNumOccurrences = DCount("ID", "tbDescriptions", strWhereCond) ' Count keyword occurences in the descriptions table
If intNumOccurrences > 0 Then ' If keyword was found at least once
With rsErrors ' Inser error code into Errors table
.AddNew
.Fields("ErrorCode") = rsKeyWords!ErrorCode
.Update
End With
End If
rsKeyWords.MoveNext ' Move to next keyword
Loop
'
' Close all and free memory
'
rsErrors.Close
rsKeyWords.Close
Set rsErrors = Nothing
Set rsKeyWords = Nothing
End Sub
再见:-)
维兹
我正在使用 Access 2010。我有一个数据库,用于记录流程中错误的自由格式文本。我想创建一个函数,用于在自由格式文本中搜索关键字并根据错误代码进行报告。虽然不确定如何去做:
前:
ID | 降序
1 |松散部分
2 |油漆污迹
3 |没有螺栓
与另一个table
中的关键字进行比较关键字 | 错误代码
油漆 | 32
螺栓 | 25
部分 | 55
输出看起来像
ID | 错误代码
1 | 55
2 | 32
3 | 25
我的想法是我可以更新关键字 table 以包含新关键字和错误代码作为更多弹出窗口。
我给你一个可能的解决方案:
假设您的 table 名称为 tbDescriptions、tbKeywords、tbErrors。 tbErrors 的 ID 字段是自动编号的(如果不是,您只需在重建期间添加 ID 值)。
这是一个子程序,您可以使用它来重建错误 table。 请注意,我假设 tbErrors 可以在您每次需要更新时删除并重建。如果不是这样,您需要管理 tbErrors 中 Error 的存在,但这并不复杂。
Private Sub RebuildErrorTable()
Dim rsKeyWords As dao.Recordset, rsErrors As dao.Recordset
Dim strKeyword As String
Dim strSELECT As String
Dim strWhereCond As String
Dim intNumOccurrences As Integer
'
' Delete all records from tbErrors
'
DoCmd.SetWarnings False
DoCmd.RunSQL = "DELETE * FROM tbErrors"
DoCmd.SetWarnings True
'
' Rebuild table tbErrors
'
strSELECT = "SELECT * FROM tbKeyWords"
Set rsKeyWords = CurrentDb.OpenRecordset(strSELECT, dbOpenDynaset) ' Open keywords table
strSELECT = "SELECT * FROM tbErrors"
Set rsErrors = CurrentDb.OpenRecordset(strSELECT, dbOpenDynaset) ' Open Errors table for rebuilding
'
' Scan all keywords
'
Do While Not rsKeyWords.EOF
strKeyword = rsKeyWords!keywords ' Current keyword
strWhereCond = "Desc LIKE '*" + strKeyword + "*'" ' Build search condition
intNumOccurrences = DCount("ID", "tbDescriptions", strWhereCond) ' Count keyword occurences in the descriptions table
If intNumOccurrences > 0 Then ' If keyword was found at least once
With rsErrors ' Inser error code into Errors table
.AddNew
.Fields("ErrorCode") = rsKeyWords!ErrorCode
.Update
End With
End If
rsKeyWords.MoveNext ' Move to next keyword
Loop
'
' Close all and free memory
'
rsErrors.Close
rsKeyWords.Close
Set rsErrors = Nothing
Set rsKeyWords = Nothing
End Sub
再见:-) 维兹