如何遍历2个记录集以在电子邮件中创建HTML表

问题描述 投票:0回答:1

我正在使用Office 365,并尝试从Access数据库创建HTML电子邮件。我想生成一个表,该表遍历2个记录集。第一个记录集是显示“问题”的查询,第二个记录集是显示两者之间存在一对多关系的问题对策的查询。这就是我要显示表格的方式:

<TABLE>
    <TH>Header from Access Table 1 – issue #1</TH>
    <TR>Row from Access Table 1– issue #1</TR>
    <TH>Header from Access Table 2 – CM # 1</TH>
    <TR>Row from Access Table 2 – CM # 1</TR>
</TABLE>

<TABLE>
    <TH>Header from Access Table 1 – issue #2</TH>
    <TR>Row from Access Table 1– issue #2</TR>
    <TH>Header from Access Table 2 – CM # 2</TH>
    <TR>Row from Access Table 2 – CM # 2</TR>
</TABLE>

但是我得到:

<TABLE>
    <TH>Header from Access Table 1 – issue #1</TH>
    <TR>Row from Access Table 1– issue #1</TR>
</TABLE>
<TABLE>
    <TH>Header from Access Table 1 – issue #2</TH>
    <TR>Row from Access Table 1– issue #2</TR>
</TABLE>
<TABLE>
     <TH>Header from Access Table 2 – CM # 2</TH>
     <TR>Row from Access Table 2 – CM # 2</TR>
</TABLE>

有人可以指出正确的方向以使我的代码正确循环吗?

     strSQL2 = "SELECT * FROM qryZoneIssue " & _
                    " WHERE qryZoneIssue.ZoneMeetingID = " & HoldMeetingID

                '*********** display issues & CMs
                    Set rs2 = CurrentDb.OpenRecordset(strSQL2, dbOpenDynaset)
                    rs2.MoveFirst
                    Do While Not rs2.EOF

           strBody2 = strBody2 & "<TABLE Border=""1"", Cellspacing=""0""><TR>" & _
           "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">No.&nbsp;</p></Font></TH>" & _
           "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">KPI&nbsp;</p></Font></TH>" & _
           "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Ranking&nbsp;</p></Font></TH>" & _
           "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">RPS Started&nbsp;</p></Font></TH>" & _
           "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Issue Details&nbsp;</p></Font></TH>" & _
              "</TR>"
                                ' add the data to the table
                                 strBody2 = strBody2 & "<TR>" & _
                     "<TD align=center width=""10%"">" & rs2.Fields![ZoneIssueNo].Value & "</TD>" & _
                     "<TD align=center width=""15%"">" & rs2.Fields![ZoneKPI].Value & "</TD>" & _
                     "<TD align=center width=""10%"">" & rs2.Fields![ZoneRankID].Value & "</TD>" & _
                     "<TD align=center width=""15%"">" & rs2.Fields![RPSStarted].Value & "</TD>" & _
                     "<TD align=left width=""50%"">" & rs2.Fields![ZoneIssue].Value & "</TD>" & _
                                 "</TR>"

            strSQL3 = "SELECT * FROM qryZonePermCM " & _
                    " WHERE qryZonePermCM.ZoneMeetingID = " & HoldMeetingID & "AND ZoneIssueID = " & rs2.Fields![ZoneIssueID].Value

                            strBody2 = strBody2 & "</table>"
                            rs2.MoveNext
                            Loop

                    Set rs3 = CurrentDb.OpenRecordset(strSQL3, dbOpenDynaset)
                    rs3.MoveFirst
                    Do While Not rs3.EOF

           strBody2 = strBody2 & "<TABLE Border=""1"", Cellspacing=""0""><TR>" & _
       "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">CM Details&nbsp;</p></Font></TH>" & _
       "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Responsible&nbsp;</p></Font></TH>" & _
       "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Target Date&nbsp;</p></Font></TH>" & _

                                ' add the data to the table
                                 strBody2 = strBody2 & "<TR>" & _
                     "<TD align=left width=""40%"">" & rs3.Fields![CMDetails].Value & "</TD>" & _
                     "<TD align=center width=""15%"">" & rs3.Fields![Responsible].Value & "</TD>" & _
                     "<TD align=center width=""10%"">" & rs3.Fields![TargetDate].Value & "</TD>" & _
                                 "</TR>"
                        strBody2 = strBody2 & "</table>"
                       rs3.MoveNext
                            Loop
                        rs3.Close
html loops ms-access access-vba
1个回答
0
投票
不要为第二个记录集数据启动表。

考虑:

strSQL2 = "SELECT * FROM qryZoneIssue WHERE ZoneMeetingID = " & HoldMeetingID '*********** display issues & CMs Set rs2 = CurrentDb.OpenRecordset(strSQL2, dbOpenDynaset) rs2.MoveFirst Do While Not rs2.EOF strBody2 = strBody2 & "<TABLE Border=""1"", Cellspacing=""0""><TR>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">No.&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">KPI&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Ranking&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">RPS Started&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Issue Details&nbsp;</p></Font></TH>" & _ "</TR>" ' add the data to the table strBody2 = strBody2 & "<TR>" & _ "<TD align=center width=""10%"">" & rs2.Fields![ZoneIssueNo].Value & "</TD>" & _ "<TD align=center width=""15%"">" & rs2.Fields![ZoneKPI].Value & "</TD>" & _ "<TD align=center width=""10%"">" & rs2.Fields![ZoneRankID].Value & "</TD>" & _ "<TD align=center width=""15%"">" & rs2.Fields![RPSStarted].Value & "</TD>" & _ "<TD align=left width=""50%"">" & rs2.Fields![ZoneIssue].Value & "</TD>" & _ "</TR>" strSQL3 = "SELECT * FROM qryZonePermCM WHERE ZoneMeetingID = " & HoldMeetingID & " AND ZoneIssueID = " & rs2!ZoneIssueID Set rs3 = CurrentDb.OpenRecordset(strSQL3, dbOpenDynaset) strBody2 = strBody2 & "<TR>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">CM Details&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Responsible&nbsp;</p></Font></TH>" & _ "<TH Bgcolor=""#2B3856"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:14px"">Target Date&nbsp;</p></Font></TH>" & _ "</TR>" Do While Not rs3.EOF ' add the data to the table strBody2 = strBody2 & "<TR>" & _ "<TD align=left width=""40%"">" & rs3.Fields![CMDetails].Value & "</TD>" & _ "<TD align=center width=""15%"">" & rs3.Fields![Responsible].Value & "</TD>" & _ "<TD align=center width=""10%"">" & rs3.Fields![TargetDate].Value & "</TD>" & _ "</TR>" rs3.MoveNext Loop strBody1 = strBody1 & strBody2 & "</table>" strBody2 = "" rs3.Close rs2.MoveNext Loop rs2.Close 'set HTML body to strBody1

© www.soinside.com 2019 - 2024. All rights reserved.