Microsoft Access:复杂字符串搜索更新另一个表

时间:2015-10-27 18:44:50

标签: parsing ms-access recordset

我有一个链接到Access的表,用于将电子邮件的结果返回到文件夹中。所有返回的电子邮件都将回答相同的问题。我需要从此表中解析此电子邮件正文文本,并使用此数据更新另一个表的多个字段。问题是链接表使文本超级混乱。即使我将所有格式良好的电子邮件返回到表格中,它也会重新进入一个充满额外间隔的热点。我想基于链接表(LinkTable)打开一个记录集,然后以某种方式解析LinkTable.Body字段,这样我就可以用干净的数据更新另一个表。返回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

前两个的答案应该是yes或no,所以我想我可以用case语句设置代码并根据匹配我应该在我的真实表中的相应字段中放置yes或no(不确定如何处理这里的额外空间),第三个可以有任意数量的答复,但这是最后一个问题,所以在“(需要许可证类型,容器标签,其他机构文件,其他)之后的任何事情”都可以采取放在另一张桌子上。有没有人有任何想法我怎么能设置它?我有点不知所措,特别是如何处理所有额外空格以及如何在附加要求段落之后获取所有文本。提前谢谢!

我获取正文的select语句如下所示:

Set rst1 = db.OpenRecordset("SELECT Subject, Contents FROM LinkTable WHERE Subject like '*1710'")

1 个答案:

答案 0 :(得分:0)

有多种方法可以执行此操作,一种方法是使用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

如果常数部分(问题)已被更改,则此操作将失败。但所有其他直接的方法也是如此。