我創建了一個單獨的查詢中的所有行現在想使用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
什麼行?你能直到它突出違規行嗎? – dbmitch
在'intNumReturned = UBound(varRecords,2)+ 1'上添加一個調試行並在'varRecords'上添加一個監視器 - 查看它是否實際上是一個數組 – dbmitch
@dbmitch違規行是'varRecords = rst!GetRows(3)' 。這是不合理的,因爲它不會動態地使用最終生成的查詢中的行數,最近該查詢更改爲6行。 – whatwhatwhat