使用参数从MS Access查询发送电子邮件 - Visual Basic

时间:2017-12-01 11:43:44

标签: ms-access access-vba

3061错误,参数太少,2预期。

查询链 - 开头的最早查询包含" [输入区域:]" (LOCALITY表中的AREA字段)和"输入Year" (例如2015-16)(DATA表中的YEAR字段),导致包含电子邮件地址的最终查询。

希望onClick按钮事件发送电子邮件,但怀疑它没有提取前两个参数因此3061错误 - 任何建议感激不尽。

Private Sub Command566_Click()
    'Opens the current access database
     Dim db As DAO.Database
     Set db = CurrentDb
     Dim RS As DAO.Recordset
     Dim EmailAdd As String
     'Mail Message MM
     Dim MM As String
     Dim qrySQL As String

     Set db = CurrentDb   

     'Creates the SQL string - query contains just email addresses
      qrySQL = "SELECT QRY_N_M109.EMAIL FROM QRY_N_M109;"

     'creates a recordset (table) based on the sql Statement above
      Set RS = db.OpenRecordset(qrySQL, dbOpenDynaset)


      Do Until RS.EOF
          'creates the email string by reading the email from each record
          EmailAdd = EmailAdd & " ; " & RS!EMAIL
          'move next record RS!EMAIL
          RS.MoveNext
      Loop

    'creates Email body in HTML Format

     MM = "Dear Delegates,"
     MM = MM & "Blah blah blah"

     'create new email
     Set olook = CreateObject("outlook.application")

     Set oMail = olook.createitem(0)
     'Set parameters
     With oMail
         .bcc = EmailAdd
         .htmlbody = MM
         .subject = "Our title here"
         .cc = "address@address.com"
         .display
     End With
 End Sub

1 个答案:

答案 0 :(得分:1)

您可以尝试使用 SetParameter

' Creates the SQL string - query contains just email addresses.
qrySQL = "SELECT QRY_N_M109.EMAIL FROM QRY_N_M109;"

' Set parameter values.
DoCmd.SetParameter "[Enter Area:]", "'" & InputBox("Enter Area:") & "'"
DoCmd.SetParameter "[Enter Year:]", "" & InputBox("Enter Year:") & ""

' Creates a recordset (table) based on the sql Statement above.
Set RS = db.OpenRecordset(qrySQL, dbOpenDynaset)