我有一个数据库。它有一个表单,可以发送电子邮件,将查询结果附加到它(附件是一个HTML文档) 我的表单的VBA代码是:
Private Sub button_send_Click()
DoCmd.SendObject acSendQuery, Me!query_name, acFormatTXT, me!email_address, , , "Subject Line"
End Sub
我不希望我的表单附加查询结果。我需要它来将附件中的内容放入电子邮件的主体中 有没有办法用VBA做到这一点?
答案 0 :(得分:0)
使用这样的函数列出输出:
Public Function ListQuery() As String
Dim qdy As DAO.QueryDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim List As String
Set qdy = CurrentDb.QueryDefs("YourQueryName")
qdy.Parameters(0).Value = Forms!NameOfYourForm!NameOfSomeTextbox.Value
qdy.Parameters(1).Value = Forms!NameOfYourForm!NameOfOtherTextbox.Value
Set rst = qdy.OpenRecordset()
If rst.RecordCount > 0 Then
While Not rst.EOF
For Each fld In rst.Fields
List = List & fld.Value & vbTab
Next
List = List & vbCrLf
rst.MoveNext
Wend
End If
rst.Close
Set fld = Nothing
Set rst = Nothing
Set qdy = Nothing
ListQuery = List
End Function
然后:
Dim MessageText As String
MessageText = ListQuery
DoCmd.SendObject acSendNoObject, , acFormatTXT, Me!email_address, , , "Subject Line", MessageText