在 Access 2007 中如何将 sql 查询的结果打印到电子邮件正文中

In Access 2007 how to print results of sql query into the body of an email message

我有一个访问数据库,可以发送一封带有一些提醒信息的电子邮件。我想使用 sql 查询的结果来填充电子邮件的正文。现在我正在使用 GetRows() 并且能够 debug.print 我想要的数据,但是,我不熟悉如何将其放入电子邮件中。我应该尝试将其保存为数组或类似的东西吗?

我当前的代码如下所示:

'Select which serial numbers to display
sqlSerialNumbers = "SELECT serialNumber, [Item Number] FROM [Equipment on loan] WHERE Evaluation = " & MailList![ID]

Set serialDB = CurrentDb
Set serialRS = serialDB.OpenRecordset(sqlSerialNumbers)

serialVar = serialRS.GetRows(serialRS.RecordCount)
Debug.Print "Serial Number", "Part Number"
    For serialRowNum = 0 To UBound(serialVar, 2) 'Loop through each Row
        For serialColNum = 0 To UBound(serialVar, 1) 'Loop through each Column
            Debug.Print serialVar(serialColNum, serialRowNum),
        Next
    Debug.Print vbCrLf
   Next

serialRS.Close


'Open Outlook
Set MyOutlook = New Outlook.Application

'This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)

'This populates the fields 
MyMail.To = emailAddress
MyMail.Subject = Subjectline
MyMail.HTMLBody =  "I want the results of GetRows here."

这是连接字符串的一种可能方式。我也使用标准 "RecordSetPointers" (我不知道名字)。但是你可以让它适应你的for循环

'Select which serial numbers to display
sqlSerialNumbers = "SELECT serialNumber, [Item Number] FROM [Equipment on loan] WHERE Evaluation = " & MailList![ID]

Set serialDB = CurrentDb
Set serialRS = serialDB.OpenRecordset(sqlSerialNumbers)

Dim strResult as String
'initialize strResult empty    
strResult = ""

serialVar = serialRS.GetRows(serialRS.RecordCount)

If not serialRS is Nothing then 'Check null
    If not (serialRS.EOF and seriaRS.BOF) then'Check Empty
        serialRS.MoveFirst 'not neccessary but still good habit
        Do While Not serialRS.EOF
            'I use your loop here
            'You could refer to the columns by serialRS(0), serialRS(1),... 
            'or serialRS(COLUMNNAME)...
            For serialColNum = 0 To UBound(serialVar, 1) 'Loop through each Column
                strResult = strResult & serialVar(serialColNum, serialRowNum) & vbCrLf 
                'separated by linefeed
            Next
            serialRS.MoveNext 'next RS
        Loop
    End if  
End If

'Clean Up   
serialRS.Close: set serialRS = nothing
serialDB.Close: set serialDB = nothing

'Open Outlook
Set MyOutlook = New Outlook.Application

'This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)

'This populates the fields 
MyMail.To = emailAddress
MyMail.Subject = Subjectline
MyMail.HTMLBody =  strResult'"I want the results of GetRows here."