访问报告的记录源,并遍历记录集中的字段

时间:2019-03-25 11:44:23

标签: vba ms-access access-vba recordset

我有一个MS Access报告,该报告从查询中获取数据。

数据集的结构如下:

[field 1] [field 2] ... [email]

该报告的标题中有一个“发送邀请”按钮,该按钮接收[email]字段中的所有值,构建一个用分号分隔的字符串,并打开一个Outlook邀请。

我尝试使用RecordsetClone进行遍历,但是发现该方法在报表中不可用。

还有另一种方法可以完成工作吗?

2 个答案:

答案 0 :(得分:0)

 Dim rs As Recordset
 Set rs = CurrentDb.OpenRecordset(Me.RecordSource)

答案 1 :(得分:0)

好像我有一个漫长而效率低下的解决方案:

    ' Get the recordset by performing a new query
    Dim rs As DAO.Recordset: Dim MailArray As Variant
    qry = "SELECT EmployeeID FROM Registry WHERE SessionID = " & CStr(TempVars("SID").Value)
    Set rs = CurrentDb.OpenRecordset(qry)

    ' Move the recordset in an array with an esoteric formula found in StackOverflow
    With rs
        .MoveLast
        .MoveFirst
        MailArray = .GetRows(.RecordCount)
    End With

    ' Loop through the array and build the string with the email addresses separated by a semicolon
    Dim i As Integer: Dim strList As String
    If (UBound(MailArray, 2)) > 0 Then
        For i = 0 To UBound(MailArray, 2)
            strList = strList + DLookup("MailAddress", "Employee", "ID=" & (MailArray(0, i))) & ";"
        Next i
    Else
        MsgBox "No people enrolled in this session.", vbOKOnly, "Attenzione!"
    End If
    Erase MailArray

    ' Feed the string to Outlook
    8< -------------------------------------

虽然有效。

PS:非常感谢Harassed Dad的支持。