我有一个访问数据库,可以发送一封包含一些提醒信息的电子邮件。我想使用SQL查询的结果来填充电子邮件的正文。现在我正在使用GetRows()并且能够调试我想要的数据,但是,我不熟悉如何将其添加到电子邮件中。我应该尝试将其保存为数组还是类似的东西?
我目前的代码如下:
'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."
答案 0 :(得分:1)
这是串联字符串的一种可能方式。我也使用标准的“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."