我正在“创建”我的第一个循环,我复制了代码,并试图让它工作。我有循环功能,但当我尝试在循环中间执行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发布于 2020-04-17 22:16:35
它应该是:
InfoInsert = DLookup("Table", "Field", "TeamDetailsID = 39")或者,如果使用变量
InfoInsert = DLookup("Table", "Field", "TeamDetailsID = " & idteam)发布于 2020-04-19 08:44:58
所以我最近的测试告诉我,我的困难不是循环。我以这种方式测试了代码(是的,我在上面的示例中混淆了字段和表)
我在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 test1https://stackoverflow.com/questions/61273598
复制相似问题