Microsoft Access:复杂字符串搜索以更新另一个 table 中的字段
Microsoft Access: Complex string search to update field in another table
我有一个 table 链接到 return 文件夹中的电子邮件结果。 returned 中的所有电子邮件都将回答相同的问题。我需要从这个 table 解析这个电子邮件正文文本,并用这个数据更新另一个 table 的几个字段。问题是链接的 table 使文本变得非常混乱。尽管我有正在 returned 的电子邮件,所有格式都很好地格式化为 table,但它返回到访问时却是一团乱麻,充满了额外的间距。我想打开一个基于链接 table (LinkTable) 的记录集,然后以某种方式解析 LinkTable.Body 字段,以便我可以用干净的数据更新另一个 table。返回 LinkTable 的数据如下所示:
允许吗? (注意:如果是,请在附加要求部分提供具体的许可证类型)
No
植物检疫证书? (注意:如果推荐,请输入否并完成附加要求部分)
Yes
附加要求:如果不适用,请注明 NA 或留空(所需许可证类型、容器标签、其他机构文件、其他)
Double containment, The labeling or declaration must provide the following information: -The kind, variety, and origin of each lot of seed -The designation “hybrid” when the lot contains hybrid seed -If the seed was treated, the name of the substance or p
前两个的答案应该是是或否,所以我想我可以用 case 语句设置代码,并根据匹配我应该在我真实的相应字段中放置是或否 table(不确定如何处理此处的额外空格),第三个可能有任意数量的答复,但这是最后一个问题,所以在“(所需的许可证类型、容器标签、其他机构文件、其他)" 可以取出并放在另一个 table 中。有谁知道我该如何设置?我有点不知所措,尤其是如何处理所有额外的空格以及如何在附加要求段落之后获取所有文本。提前致谢!
我的 select 获取正文的语句如下所示:
Set rst1 = db.OpenRecordset("SELECT Subject, Contents FROM LinkTable WHERE Subject like '*1710'")
有多种方法可以做到这一点,一种是使用 Instr()
和 Len()
找到固定问题的开头和结尾,然后 Mid()
提取答案。
但我认为使用 Split()
更容易。最好用注释代码来解释。
Public Sub TheParsing()
' A string constant that you expect to never come up in the Contents, used as separator for Split()
Const strSeparator = "##||##"
Dim rst1 As Recordset
Dim S As String
Dim arAnswers As Variant
Dim i As Long
S = Nz(rst1!Contents, "")
' Replace all the constant parts (questions) with the separator
S = Replace(S, "Permit? (Note: if yes, provide specific permit type in Additional Requirements section)", strSeparator)
' etc. for the other questions
' Split the remaining string into a 0-based array with the answers
arAnswers = Split(S, strSeparator)
' arAnswers(0) contains everything before the first question (probably ""), ignore that.
' Check that there are 3 answers
If UBound(arAnswers) <> 3 Then
' Houston, we have a problem
Stop
Else
For i = 1 To 3
' Extract each answer
S = arAnswers(i)
' Remove whitespace: CrLf, Tab
S = Replace(S, vbCrLf, "")
S = Replace(S, vbTab, "")
' Trim the remaining string
S = Trim(S)
' Now you have the cleaned up string and can use it
Select Case i
Case 1: strPermit = S
Case 2: strCertificate = S
Case 3: strRequirements = S
End Select
Next i
End If
rst1.MoveNext
' etc
End Sub
如果更改了常量部分(问题),这将失败。但是所有其他直接的方法也是如此。
我有一个 table 链接到 return 文件夹中的电子邮件结果。 returned 中的所有电子邮件都将回答相同的问题。我需要从这个 table 解析这个电子邮件正文文本,并用这个数据更新另一个 table 的几个字段。问题是链接的 table 使文本变得非常混乱。尽管我有正在 returned 的电子邮件,所有格式都很好地格式化为 table,但它返回到访问时却是一团乱麻,充满了额外的间距。我想打开一个基于链接 table (LinkTable) 的记录集,然后以某种方式解析 LinkTable.Body 字段,以便我可以用干净的数据更新另一个 table。返回 LinkTable 的数据如下所示:
允许吗? (注意:如果是,请在附加要求部分提供具体的许可证类型)
No
植物检疫证书? (注意:如果推荐,请输入否并完成附加要求部分)
Yes
附加要求:如果不适用,请注明 NA 或留空(所需许可证类型、容器标签、其他机构文件、其他)
Double containment, The labeling or declaration must provide the following information: -The kind, variety, and origin of each lot of seed -The designation “hybrid” when the lot contains hybrid seed -If the seed was treated, the name of the substance or p
前两个的答案应该是是或否,所以我想我可以用 case 语句设置代码,并根据匹配我应该在我真实的相应字段中放置是或否 table(不确定如何处理此处的额外空格),第三个可能有任意数量的答复,但这是最后一个问题,所以在“(所需的许可证类型、容器标签、其他机构文件、其他)" 可以取出并放在另一个 table 中。有谁知道我该如何设置?我有点不知所措,尤其是如何处理所有额外的空格以及如何在附加要求段落之后获取所有文本。提前致谢!
我的 select 获取正文的语句如下所示:
Set rst1 = db.OpenRecordset("SELECT Subject, Contents FROM LinkTable WHERE Subject like '*1710'")
有多种方法可以做到这一点,一种是使用 Instr()
和 Len()
找到固定问题的开头和结尾,然后 Mid()
提取答案。
但我认为使用 Split()
更容易。最好用注释代码来解释。
Public Sub TheParsing()
' A string constant that you expect to never come up in the Contents, used as separator for Split()
Const strSeparator = "##||##"
Dim rst1 As Recordset
Dim S As String
Dim arAnswers As Variant
Dim i As Long
S = Nz(rst1!Contents, "")
' Replace all the constant parts (questions) with the separator
S = Replace(S, "Permit? (Note: if yes, provide specific permit type in Additional Requirements section)", strSeparator)
' etc. for the other questions
' Split the remaining string into a 0-based array with the answers
arAnswers = Split(S, strSeparator)
' arAnswers(0) contains everything before the first question (probably ""), ignore that.
' Check that there are 3 answers
If UBound(arAnswers) <> 3 Then
' Houston, we have a problem
Stop
Else
For i = 1 To 3
' Extract each answer
S = arAnswers(i)
' Remove whitespace: CrLf, Tab
S = Replace(S, vbCrLf, "")
S = Replace(S, vbTab, "")
' Trim the remaining string
S = Trim(S)
' Now you have the cleaned up string and can use it
Select Case i
Case 1: strPermit = S
Case 2: strCertificate = S
Case 3: strRequirements = S
End Select
Next i
End If
rst1.MoveNext
' etc
End Sub
如果更改了常量部分(问题),这将失败。但是所有其他直接的方法也是如此。