2015-09-01 89 views
0

我正在學習Access和VBA。當我嘗試解釋我正在嘗試做什麼時,請和我一起裸照。如何從Access數據庫創建和發送電子郵件

我有產生這樣一個表的查詢 -

行1 - ID 1彼得Parker任務1 $ 50

行2 - ID 1彼得Parker任務2 $ 55

行3 - ID 1彼得·帕克任務3 $ 60

行4 - ID 2瑪麗珍任務1 $ 45

行5 ...

我希望能夠發送一個電子郵件給每個人提供的任務和數量,總金額列表 -

彼得·帕克

任務1 $ 50

任務2 $ 55

任務3 $ 60

總計$ 165

我有發送電子郵件的模塊,但我t每行需要一個接收者。我想我需要另一個循環,但是我失去了如何做到這一點。

這是我現在使用的代碼 -

 Sub SendMessages(Optional AttachmentPath) 

     Dim MyDB As Database 
     Dim MyRS As Recordset 
     Dim objOutlook As Outlook.Application 
     Dim objOutlookMsg As Outlook.MailItem 
     Dim objOutlookRecip As Outlook.Recipient 
     Dim objOutlookAttach As Outlook.Attachments 
     Dim TheAddress As String 

     Set MyDB = CurrentDb 
     Set MyRS = MyDB.OpenRecordset("qry_TeacherPayment - Round 2") 
     MyRS.MoveFirst 

     ' Create the Outlook session. 
     Set objOutlook = CreateObject("Outlook.Application") 

     Do Until MyRS.EOF 

     'Set loop variables 

    Dim currentRecord As Integer 
    Dim oldRecord As Integer 
    Dim totalAmt As Double 
    currentRecord = MyRS![ID] 
    totalAmt = 0 

     If (currentRecord = MyRS![ID]) Then 

     ' Create the e-mail message. 
     Set objOutlookMsg = objOutlook.CreateItem(olMailItem) 
     oldRecord = currentRecord 
     TheAddress = MyRS![WorkEmail] 


     With objOutlookMsg 
      ' Add the To recipients to the e-mail message. 
      Set objOutlookRecip = .Recipients.Add("TheAddress") 
      objOutlookRecip.Type = olTo 



      ' Set the from address. 
      objOutlookMsg.SentOnBehalfOfName = "email" 

      ' Set the Subject, the Body, and the Importance of the e-mail message. 
      .Subject = "Subject" 

      objOutlookMsg.BodyFormat = olFormatHTML 

body text 

      .HTMLBody = .HTMLBody & "</table></body></html>" 

      .Importance = olImportanceNormal 'Normal importance 


      ' Resolve the name of each Recipient. 
      For Each objOutlookRecip In .Recipients 
       objOutlookRecip.Resolve 
       If Not objOutlookRecip.Resolve Then 
       objOutlookMsg.Display 
       End If 
      Next 
      .Send 
      End With 
      End If 
      MyRS.MoveNext 
     Loop 
     Set objOutlookMsg = Nothing 
     Set objOutlook = Nothing 
     DoCmd.SetWarnings True 
    End Sub 

任何建議將非常感激!

乾杯!

傑森

+0

到ATLEAST告訴你到目前爲止的代碼這將是有益的。這樣,人們可以給你想法或提示做什麼 – DragonSamu

+0

謝謝DragonSamu。我已經使用我已用於單行收件人的代碼編輯我的帖子。我試圖在電子郵件的正文中添加一個循環,但不斷收到錯誤3021「沒有當前記錄」。 –

回答

0

假設米兒絲將返回領域類似是PersonID,的TaskID,TaskAmt,您可以通過米兒絲需要循環,並添加任務和AMT爲一個字符串變量(例如「strBody」),直到爲PERSONID的變化 - 在您準備並使用objOutlookMsg發送電子郵件。

Set MyRS = ... 
LastPersonID=MyRS!PersonID 
Do Until MyRS.EOF 
    If MyRS!PersonID=LastPersonID Then 
     ' concatenate to strBody 
     strBody = strBody & TaskID & "&nbsp;&nbsp;" & TaskAmt 

     ' add Amt to Person's Total 
     decTotal = decTotal + nz(TaskAmt,0) 
    Else 
     ' add the Total 
     strBody = strBody & "Total:&nbsp;&nbsp;" & decTotal 

     ' send email using another function, or GoTo a named line, 
     ' using LastPersonID and strBody 
     GoTo send_email 

     ' reset the variables 
     strBody = "" 
     decTotal = 0 

     ' concatenate to strBody 
     strBody = strBody & TaskID & "&nbsp;&nbsp;" & TaskAmt & "<br/>" 
     ' add Amt to Person's Total 
     decTotal = decTotal + nz(TaskAmt,0) 
    End If 

    MyRS.MoveNext 
Loop 
+0

感謝maxhugen,這是一個巨大的幫助!我發現我必須在循環後再次調用發送電子郵件功能,以確保最後一條記錄包含在最後一封電子郵件中。非常感謝!! –

1

這是我的最終代碼作品。非常感謝maxhugen讓我走上正確的道路!

乾杯!

傑森

Sub SendNewPaymentEmail(Optional AttachmentPath) 

     Dim MyDB As Database 
     Dim MyRS As Recordset 
     Dim LastTeacherID As Integer 
     Dim EmailBody As String 
     Dim TotalAmount As Double 
     Dim TheAddress As String 
     Dim TeacherFirstName As String 
     Dim FinalTeacherID As Integer 

     Set MyDB = CurrentDb 
     Set MyRS = MyDB.OpenRecordset("qry_TeacherPayment - Round 2") 
     MyRS.MoveFirst 

     LastTeacherID = MyRS![ID] 

     Do Until MyRS.EOF 

     If MyRS![ID] = LastTeacherID Then 

      TheAddress = MyRS![WorkEmail] 
      TeacherFirstName = MyRS![FirstName] 
      FinalTeacherID = MyRS![TeacherID] 
      EmailBody = EmailBody & "<tr><td>" & MyRS![Subject] & " Year " & MyRS![Year] & "</td><td>" & MyRS![TaskName] & "</td><td>$" & MyRS![Teacher Payment] & "</td></tr>" 
      TotalAmount = TotalAmount + Nz(MyRS![Teacher Payment], 0) 

      DoCmd.SetWarnings False 
      DoCmd.RunSQL "Update [tbl_Judging Standards Project round 2] SET [PaymentEmailSent] = -1 WHERE [TeacherID] = " & MyRS![ID] & " AND [TaskIDTRIM] LIKE '" & MyRS![TaskIDTRIM] & "'" 
      DoCmd.RunSQL "Update [tbl_Judging Standards Project round 2] SET [PaymentEmailSentDate] = Now() WHERE [TeacherID] = " & MyRS![ID] & " AND [TaskIDTRIM] LIKE '" & MyRS![TaskIDTRIM] & "'" 
      DoCmd.SetWarnings True 

     Else 

      Call CreateEmail(EmailBody, TotalAmount, TeacherFirstName) 

      DoCmd.SetWarnings False 
      DoCmd.RunSQL "INSERT INTO [tbl_Payments]([TeacherID],[PaymentType],[Amount],[Description],[PaymentFormSent],[PaymentFormSentDate]) VALUES(" & FinalTeacherID & ", 'Individual Payment'," & TotalAmount & ",'Judging Standards Project Phases 2 and 3 - Payment for work samples - Round 2', -1, NOW())" 
      DoCmd.SetWarnings True 

      'reset variables 
      EmailBody = "" 
      TotalAmount = 0 


      'start again 
      TheAddress = MyRS![WorkEmail] 
      TeacherFirstName = MyRS![FirstName] 
      LastTeacherID = MyRS![ID] 
      FinalTeacherID = MyRS![TeacherID] 
      EmailBody = EmailBody & "<tr><td>" & MyRS![Subject] & " Year " & MyRS![Year] & "</td><td>" & MyRS![TaskName] & "</td><td>$" & MyRS![Teacher Payment] & "</td></tr>" 
      TotalAmount = TotalAmount + Nz(MyRS![Teacher Payment], 0) 

      DoCmd.SetWarnings False 
      DoCmd.RunSQL "Update [tbl_Judging Standards Project round 2] SET [PaymentEmailSent] = -1 WHERE [TeacherID] = " & MyRS![ID] & " AND [TaskIDTRIM] LIKE '" & MyRS![TaskIDTRIM] & "'" 
      DoCmd.RunSQL "Update [tbl_Judging Standards Project round 2] SET [PaymentEmailSentDate] = Now() WHERE [TeacherID] = " & MyRS![ID] & " AND [TaskIDTRIM] LIKE '" & MyRS![TaskIDTRIM] & "'" 
      DoCmd.SetWarnings True 

     End If 

     MyRS.MoveNext 

     If (MyRS.EOF) Then 

     Call CreateEmail(EmailBody, TotalAmount, TeacherFirstName) 

     DoCmd.SetWarnings False 
     DoCmd.RunSQL "INSERT INTO [tbl_Payments]([TeacherID],[PaymentType],[Amount],[Description],[PaymentFormSent],[PaymentFormSentDate]) VALUES(" & FinalTeacherID & ", 'Individual Payment'," & TotalAmount & ",'Judging Standards Project Phases 2 and 3 - Payment for work samples - Round 2', -1, NOW())" 
     DoCmd.SetWarnings True 

     End If 

    Loop 

End Sub 
相關問題