2017-02-24 36 views
1

我試圖將我的一個查詢導出爲使用VBA格式的電子郵件。與您轉到外部數據並單擊和電子郵件時類似,它會向Outlook添加附件。除了我想要的身體。我把下面的代碼放在一個按鈕中。將查詢中的表導出到電子郵件VBA

我發現並對一些代碼做了一些更改。這是我的。

Private Sub Command5_Click() 
Dim olApp As Object 
Dim olItem As Variant 
Dim db As DAO.Database 
Dim rec As DAO.Recordset 
Dim strQry As String 
Dim aHead(1 To 4) As String 
Dim aRow(1 To 4) As String 
Dim aBody() As String 
Dim lCnt As Long 

'Create the header row 
aHead(1) = "Part" 
aHead(2) = "Description" 
aHead(3) = "Qty" 
aHead(4) = "Price" 

lCnt = 1 
ReDim aBody(1 To lCnt) 
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th> <th>") & "</th></tr>" 

'Create each body row 
strQry = "SELECT * From qry_email" 
Set db = CurrentDb 
Set rec = CurrentDb.OpenRecordset(strQry) 

If Not (rec.BOF And rec.EOF) Then 
Do While Not rec.EOF 
    lCnt = lCnt + 1 
    ReDim Preserve aBody(1 To lCnt) 
    aRow(1) = rec("Part") 
    aRow(2) = rec("Description") 
    aRow(3) = rec("Qty") 
    aRow(4) = rec("Price") 
    aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>" 
    rec.MoveNext 
Loop 
End If 

aBody(lCnt) = aBody(lCnt) & "</table></body></html>" 

'create the email 
Set olApp = CreateObject("Outlook.application") 
Set olItem = olApp.CreateItem(0) 

olItem.Display 
olItem.To = "[email protected]" 
olItem.Subject = "Test E-mail" 
olItem.HTMLBody = Join(aBody, vbNewLine) 
olItem.Display 

End Sub 

當我運行代碼時,我得到一個「運行時錯誤'3061'太少參數。預期1」。

如果我點擊調試,我會以黃色突出顯示。任何人的幫助將不勝感激!

編輯

我想這其實給我的名單中電子郵件的正文中不同的方法。但它對整個表格而言,而不僅僅是我想要的一個記錄。這就是SQL查詢的樣子。

SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty,  tblePMParts.Price 
FROM tblePMParts 
WHERE (((tblePMParts.WOID)=[Forms]![fmremail]![Text1])); 

我該如何去添加WHERE到下面的代碼。

Private Sub Command4_Click() 


'On Error GoTo Errorhandler 

Dim olApp As Object 
Dim olItem As Variant 
Dim olatt As String 
Dim olMailTem As Variant 
Dim strSendTo As String 
Dim strMsg As String 
Dim strTo As String 
Dim strcc As String 
Dim rst As DAO.Recordset 
Dim rs As DAO.Recordset 
Dim db As DAO.Database 
Dim qry As DAO.QueryDef 
Dim fld As Field 
Dim varItem As Variant 
Dim strtable As String 
Dim rec As DAO.Recordset 
Dim strQry As String 




strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _ 
     "FROM tblePMParts; " 

strSendTo = "[email protected]" 

strTo = "" 
strcc = "" 

Set olApp = CreateObject("Outlook.application") 
Set olItem = olApp.CreateItem(olMailTem) 

olItem.Display 
olItem.To = strTo 
olItem.CC = strcc 
olItem.Body = "" 
olItem.Subject = "Please Quote the Following!" 

Set db = CurrentDb 
Set rec = CurrentDb.OpenRecordset(strQry) 
If Not (rec.BOF And rec.EOF) Then 
    rec.MoveLast 
    rec.MoveFirst 
    intCount = rec.RecordCount 
     For intLoop = 1 To intCount 
      olItem.Body = olItem.Body & rec("[Part#]") & " - " &  rec("PartDescription") & " - " & rec("Qty") & " - " & rec("Price") 
      rec.MoveNext 
     Next intLoop 
End If 

MsgBox "Completed Export" 
Set olApp = Nothing 
Set olItem = Nothing 

Exit_Command21_Click: 
Exit Sub 
ErrorHandler: 
MsgBox Err.Description, , Err.Number 
Resume Exit_Command21_Click 

End Sub 

我明白了。這裏是任何人需要的代碼。

Private Sub Command5_Click() 
Dim olApp As Object 
Dim olItem As Variant 
Dim db As DAO.Database 
Dim rec As DAO.Recordset 
Dim strQry As String 
Dim aHead(1 To 3) As String 
Dim aRow(1 To 3) As String 
Dim aBody() As String 
Dim lCnt As Long 

'Create the header row 
aHead(1) = "Part#" 
aHead(2) = "Description" 
aHead(3) = "Qty" 

lCnt = 1 
ReDim aBody(1 To lCnt) 
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>" 

'Create each body row 
strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _ 
"FROM tblePMParts " & _ 
"WHERE (((tblePMParts.WOID)=" & [Forms]![fmremail]![Text1] & "));" 
Set db = CurrentDb 
Set rec = CurrentDb.OpenRecordset(strQry) 

If Not (rec.BOF And rec.EOF) Then 
    Do While Not rec.EOF 
     lCnt = lCnt + 1 
     ReDim Preserve aBody(1 To lCnt) 
     aRow(1) = rec("[Part#]") 
     aRow(2) = rec("PartDescription") 
     aRow(3) = rec("Qty") 
     aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>" 
     rec.MoveNext 
    Loop 
End If 

aBody(lCnt) = aBody(lCnt) & "</table></body></html>" 

'create the email 
Set olApp = CreateObject("Outlook.application") 
Set olItem = olApp.CreateItem(0) 

olItem.Display 
olItem.To = "Email" 
olItem.Subject = "Test E-mail" 
olItem.HTMLBody = Join(aBody, vbNewLine) 
olItem.Display 

End Sub 
+0

qry_email運行嗎? – Fionnuala

+0

@Fionnuala對不起,我是使用VBA的新手,但我怎麼會告訴qry_email實際上在運行。我用不同的方法編輯了原文。該方法的工作原理,但我不知道如何將WHERE部分添加到代碼。 – Luis

回答

0

某處在你的代碼,把這樣一行

X = [Forms]![fmremail]![Text1] 

將斷點在你的代碼(希望你知道怎麼做嗎?)在該行。當代碼中斷時,按F8進入下一行,然後在立即窗口中輸入?X。或者,您可以將鼠標懸停在帶有折點的線上。關鍵是,你需要看看你的代碼認爲[Forms]![fmremail]![Text1]等於什麼。如果它爲空,則說明您的參考存在問題。在這種情況下,您可能需要在其末尾添加「.Value」或「.Text」。

另一件要檢查的是你的WOID數據類型。如果是文本,則需要用單引號括起來。

strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _ 
    "FROM tblePMParts " & _ 
    "WHERE (((tblePMParts.WOID)='" & [Forms]![fmremail]![Text1] & "'));" 
+0

我將其更改爲特定記錄,並且工作得非常好。例如,這工作「WHERE tblePMParts.WOID = 907;」 。擁有文本框不是我嘗試添加單引號,它不起作用。 Text1是表單中的文本框。 – Luis

-1

我要鏈接你這個優秀的答案:https://stackoverflow.com/a/8450437/3689364

「運行時錯誤 '3061' 參數太少預期1。」

我相信這個時候字段名(S)在您的SQL查詢不 匹配表中的字段名(一個或多個),即在查詢的字段名稱錯誤 或者是表全部丟失現場情況發生。

您的記錄集查詢必須是錯誤的。在SQL編輯器中嘗試查詢,替換所有參數以獲取實際值。如果錯誤再次發生,訪問將突出顯示查詢的錯誤部分。

+0

我將strQry =「SELECT * from qry_email」改爲strQry =「SELECT tblePMParts。[Part#],tblePMParts.PartDescription,tblePMParts.Qty,tblePMParts.Price」&_ 「FROM tblePMParts;」它顯示運行時錯誤'94'空值的使用無效。並突出顯示aRow(1)= rec(「[Part#]」) – Luis

相關問題