1

我正在“创建”我的第一个循环,我复制了代码并试图让它工作。我有循环功能,但是当我尝试在循环中间进行 Dlookup 时,它不起作用。

我确信有一些方法可以让这段代码更好地工作,只是试图检索我的电子邮件正文的动态数据。

这是循环的相关部分。

strSQL = "SELECT * FROM emailbody Where EmailMainID = " & Me.EmailMainID

Set rs = CurrentDb.OpenRecordset(strSQL)

With rs
    If Not .BOF And Not .EOF Then

            .MoveLast
            .MoveFirst

            While (Not .EOF)
                LookupInfo = rs.Fields("beforetable") & "-" & rs.Fields("beforefield") 'Get Table and Field to lookup
                LookupLen = Len(LookupInfo) 'Find how many letters are in the string
                SubtractLen = InStr(1, [LookupInfo], "-") ' Find the number of letters to the left
                RightCut = LookupLen - SubtractLen ' Find how many are to the right
                Table = Left([LookupInfo], InStr(1, [LookupInfo], "-") - 1) ' Set the table value
                Field = Right([LookupInfo], RightCut) ' Set the Field Value
                InfoInsert = DLookup("Table", "Field", TeamDetailsID = 39)
                FreshData = rs.Fields("emailbodyid") & " " & rs.Fields("bodycontent") 

                LongEmail = EmailMe & FreshData
                EmailMe = LongEmail
                FreshData = ""
                LongEmail = ""

              .MoveNext
            Wend

        End If

        .Close

    End With
4

2 回答 2

2

它应该是:

InfoInsert = DLookup("Table", "Field", "TeamDetailsID = 39")

或者,如果您使用变量

InfoInsert = DLookup("Table", "Field", "TeamDetailsID = " & idteam)
于 2020-04-17T14:16:35.217 回答
0

所以我最近的测试告诉我我的困难不是循环。我以这种方式测试了代码(是的,我在上面的示例中混淆了字段和表格)

我在 Test2 中得到结果,但不是在 Test1

Dim Table As String
Dim Field As String

Table = TeamDetails
Field = DepartDate

test2 = DLookup("DepartDate", "TeamDetails", "TeamDetailsID = 39")
MsgBox test2
test1 = DLookup("Field", "Table", "TeamDetailsID = 39")
MsgBox test1
于 2020-04-19T00:44:58.237 回答