Access 2010でDoCmd.SendObjectを使用してメールを自動生成したいのですが、実行時エラー3061でとまってしまいます。デバッグすると、Set RS = DB.OpenRecordset("30DaysQuery")でとまっています。生成するメールは1通、その本文に複数データを列記という形です。イメージとしてはこんな感じです。→<http://www.tsware.jp/tips/tips_464.htm>
大した知識もなく作っています。言葉足らず、情報足らずの点があれば、ご指摘ください。よろしくお願いします。
Private Sub EmailReminder_Click()
Dim DB As Database
Dim RS As Recordset
Dim Subject As String
Dim Body As String
Subject = "Audit Corrective Actions"
Body = "Good Morning Sir/Ma'am," & vbCrLf _
& "This is an auto generated email." & vbCrLf _
& "In response to a recent audit, your attention is needed for the following item(s)." & vbCrLf & vbCrLf _
& "Please advise when these actions are completed. If you have any questions please feel free to contact our office. Thank you for your help and cooperation in this matter. Have a nice day." & vbCrLf & vbCrLf _
& "Corrective Actions:" & vbCrLf _
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("30DaysQuery")
Do Until RS.EOF
Body = Body &
"------------------------------------------------------" & vbCrLf _
& "[Due Date: " & RS!DueDate & "] [Agency: " & RS!Agency & "] [Report: " & RS!Report# & "]" & vbCrLf _
& "Corrective Action: " & RS!CorrectiveAction & vbCrLf _
& "Recommendation: " & RS!Recommendation & vbCrLf
RS.MoveNext
Loop
DoCmd.SendObject , , acFormatTXT, RS!Email, "***@bbb.com", ,
Subject, Body, True
RS.Close
Resume Next
End Sub
回答者は、エラー番号とその内容を覚えているわけではありません。
エラーメッセージを正確に書くようにしましょう。
で、回答は、下記を参考にしてください。
[AC95] モジュール実行時「パラメータが少なすぎます。」エラー
http://support.microsoft.com/kb/404918/ja
フォーム参照のパラメータクエリをVBAで扱う方法
http://www.tsware.jp/tips/tips_586.htm
なお、参照先の
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
は、上位オブジェクトから
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
のように記述する方がベターだと思います。
Access のバージョンによっては別のエラーが発生する可能性がありますので。
あと、パッと見で気になる点として
数字で始まるオブジェクト名はトラブルの元です。
可能なら英字から始まるように変更しましょう。
質問者
お礼
コメントありがとうございます。番号の後の メッセージのほうが重要だと分かっていません でした。すみませんでした。
参考リンクの「フォーム参照のパラメータクエ リ・・」というのがまさに当てはまったと思い ます。
下記のように書き換えたところ、「エラー3265 このコレクションには項目がありません」とい うエラーに変わり、
Body = Body & "------------------------------------------------------" & vbCrLf _
& "[Due Date: " & RS!DueDate & "] [Agency: " & RS!Agency & "] [Report: " & RS!Report# & "]" & vbCrLf _
& "Corrective Action: " & RS!CorrectiveAction & vbCrLf _
& "Recommendation: " & RS!Recommendation & vbCrLf
で止まるようになりました。Reportという フォームを開いた状態で、クエリを開けば、 データは呼び出されています。よろしくお願い します。
Private Sub EmailReminder_Click()
Dim DB As DAO.Database
Dim QD As DAO.QueryDef
Dim RS As DAO.Recordset
Dim Subject As String
Dim Body As String
Subject = "Audit Corrective Actions"
Body = "Good Morning Sir/Ma'am," & vbCrLf _
& "This is an auto generated email." & vbCrLf _
& "Please advise when these actions are completed. If you have any questions please feel free to contact our office. Thank you for your help and cooperation in this matter. Have a nice day." & vbCrLf & vbCrLf _ & "Corrective Actions:" & vbCrLf _
Set DB = CurrentDb()
Set QD = DB.QueryDefs("ReminderQuery")
With QD
.Parameters("[Forms]![Report]![ReportID]") = Forms!Report!ReportID
Set RS = .OpenRecordset
Do Until RS.EOF
Body = Body & "------------------------------------------------------" & vbCrLf _
& "[Due Date: " & RS!DueDate & "] [Agency: " & RS!Agency & "] [Report: " & RS!Report# & "]" & vbCrLf _
& "Corrective Action: " & RS!CorrectiveAction & vbCrLf _
& "Recommendation: " & RS!Recommendation & vbCrLf
RS.MoveNext
Loop
DoCmd.SendObject , , acFormatTXT, RS!Email, "aaa@aaa.com", , Subject, Body, True
.Close
End With
End Sub
気になったところにコメント入れてます。
Dim DB As Database
Dim RS As dao.Recordset 'DAO のレコードセットを使用する
Dim Subject As String
Dim Body As String
Subject = "Audit Corrective Actions"
Body = "Good Morning Sir/Ma'am," & vbCrLf _
& "This is an auto generated email." & vbCrLf _
& "In response to a recent audit, your attention is needed for the following item(s)." & vbCrLf & vbCrLf _
& "Please advise when these actions are completed. If you have any questions please feel free to contact our office. Thank you for your help and cooperation in this matter. Have a nice day." & vbCrLf & vbCrLf _
& "Corrective Actions:" '& vbCrLf _ '行継続文字が中途半端に終わっている
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("30DaysQuery") 'クエリ名ではなくてテーブルを指定した場合で試す
Do Until RS.EOF
Body = Body & vbCrLf & _
"------------------------------------------------------" & vbCrLf _
& "[Due Date: " & RS!DueDate & "] [Agency: " & RS!Agency & "] [Report: " & RS!Report# & "]" & vbCrLf _
& "Corrective Action: " & RS!CorrectiveAction & vbCrLf _
& "Recommendation: " & RS!Recommendation & vbCrLf
RS.MoveNext
Loop
DoCmd.SendObject , , acFormatTXT, RS!Email, "***@bbb.com", , Subject, Body, True
RS.Close: Set RS = Nothing
'Resume Next 'これは何のため?
お礼
またも、どんぴしゃです。ありがとうございま す。 自分でも「Report#」というフィールド名はよく ないような気になって、#をやめたのですが、変 わらず。。 ご指摘のフォーム名を変えたら動きました!予 約語というものがあるのなら、エラーとなって 弾いてくれたらいいのになと素人は思いまし た。。^^; 動きました!と言っても、また別の箇所でエ ラーが起きてしまいました。。1件の質問で、い くつものエラーについて尋ねて、ずるいように 思えてきたので、別件として質問を作成しま す。またよろしくお願いします。