2016-06-27 37 views
1

我創建了一個單獨的查詢中的所有行現在想使用VBA來讀取其記錄,然後送所有一定領域在一封電子郵件中收到一個錯誤。當試圖讀取記錄

我目前停留在試圖從記錄集中提取所有行。我知道如何做一個記錄,但不是動態記錄集。每個星期,記錄集可能有1-10(約)個記錄。我曾希望通過動態讀取所有行,將所需的字段保存到變量中,然後將其添加到電子郵件正文中,但我遇到了一個錯誤。

我收到一個錯誤,指出:Run-time error '3265': Item not found in this collection.

有誰知道如何解決這個錯誤,我怎麼可以把所有產生的記錄行到電子郵件正文中?

代碼:

Private Sub Form_Timer() 

    'current_date variable instantiated in a module elsewhere 
    current_date = Date 

    'Using the Date function to run every Monday, regardless of the time of day 
    If current_date = (Date - (DatePart("w", Date, 2, 1) - 1)) Then 

     'MsgBox ("the current_date variable holds: " & current_date) 

     Dim dbs As DAO.Database 
     Dim rst As DAO.Recordset 
     Dim qdf As DAO.QueryDef 
     Dim prm As DAO.Parameter 
     Dim varRecords As Variant 
     Dim intNumReturned As Integer 
     Dim intNumColumns As Integer 
     Dim intColumn As Integer 
     Dim intRow As Integer 
     Dim strSQL As String 
     Dim rst_jobnumber As String 
     Dim rst_bfloc As String 

     Set dbs = CurrentDb 
     Set qdf = dbs.QueryDefs("qry_BMBFLoc") 
     Set rst = qdf.OpenRecordset 

     If rst.EOF Then 

      MsgBox "Null." 

     Else 

      'Found this part of the code online and not sure if I'm using it right. 
      varRecords = rst!GetRows(3) 
      intNumReturned = UBound(varRecords, 2) + 1 
      intNumColumns = UBound(varRecords, 1) + 1 

      For intRow = 0 To intNumReturned - 1 
       For intColumn = 0 To intNumColumns - 1 
       Debug.Print varRecords(intColumn, intRow) 
       Next intColumn 
      Next intRow 
      'End of code found online. 

      'rst.MoveFirst 'commenting this out because this query could potentially return multiple rows 
      rst_jobnumber = rst!job & "-" & rst!suffix 
      rst_bfloc = rst!Uf_BackflushLoc 

      rst.Close 
      dbs.Close 

      Set rst = Nothing 
      Set dbs = Nothing 

      'Dim oApp As Outlook.Application 
      'Dim oMail As MailItem 

      'Set oApp = CreateObject("Outlook.application") 

      'mail_body = "The following jobs do not have the special BF location set in Job Orders: " & rst_ 

      'Set oMail = oApp.CreateItem(olMailItem) 
      'oMail.Body = mail_body 
      'oMail.Subject = "Blow Molding Jobs Missing BF Location" 
      'oMail.To = "[email protected]" 'in the future, create a function that finds all of the SC users' emails from their Windows user 
      'oMail.Send 

      'Set oMail = Nothing 
      'Set oApp = Nothing 

     End If 

    End If 

ErrorHandler: 
    MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description 

End Sub 
+0

什麼行?你能直到它突出違規行嗎? – dbmitch

+0

在'intNumReturned = UBound(varRecords,2)+ 1'上添加一個調試行並在'varRecords'上添加一個監視器 - 查看它是否實際上是一個數組 – dbmitch

+0

@dbmitch違規行是'varRecords = rst!GetRows(3)' 。這是不合理的,因爲它不會動態地使用最終生成的查詢中的行數,最近該查詢更改爲6行。 – whatwhatwhat

回答

1

嘗試使用此代碼的工作,看看它是如何工作的,你行。我不確定你是否發送一封電子郵件每封郵件或一封電子郵件全部(我假設後者)

Dim dbs As DAO.Database 
Dim rst As DAO.Recordset 
Dim strMessageBody As String 

Set dbs = CurrentDb 
Set rst = CurrentDb.OpenRecordset("qry_BMBFLoc") 

strMessageBody = "The following jobs do not have the special BF location set in Job Orders: " 

If Not (rst.EOF And rst.BOF) Then 
    rst.MoveFirst 
    Do Until rst.EOF = True   
      strMessageBody = strMessageBody & rst!job & "-" & rst!suffix & "," 
    rst.MoveNext 
    Loop 

    If Right(strMessageBody, 1) = "," Then strMessageBody = Left(strMessageBody, Len(strMessageBody)-1) 
    End If 
    rst.Close 

Set rst = Nothing 
Set dbs = Nothing 
+0

這個效果很好,但你會有關於如何將輸出字符串放入列表的提示嗎?我在電子郵件中獲得的輸出是'以下作業沒有在作業單:BM00001610-27BM00001608-27BM00001633-27BM0001608A-27BM00001612-27BM00001634-27'中設置的特殊BF位置。它看起來很亂... – whatwhatwhat

+0

沒關係,明白了。只需要使用'vbCrLf'。非常感謝您的時間和耐心! – whatwhatwhat

0

編輯 - 不使用點運算符

更換

varRecords = rst!GetRows(3) 

varRecords = rst.GetRows(3) 

你的記錄集中有三行嗎?

如果不是rst!GetRows(3)將返回false - 然後當您嘗試使用UBound時下一行將失敗。

A good example of how to implement GetRows

另一種可能性是,如果你想訪問一個字段,這不是在您的記錄上有rst!