shawngert
asked on
"For each" statement VBA - MS access calling a lotus notes odbc connection
have an access db with an odbc connection to lotus notes mailbox. VBA code automatically runs query and then sends results to sender of email received in timed intervals. Trying to incorporate a field in the query to "group" the whole looping process, so basically to run the whole VBA process for each ML_SUPV_EMPLID in this case. VBA code:
Function Email()
Dim UserName As String
Dim MailDbName As String
Dim Maildb As Object
Dim MailDoc As Object
Dim AttachME As Object
Dim session As Object
Dim EmbedObj1 As Object
Dim Eml2 As String
Dim Reports_To_Name As String
Dim Recp As String
Dim Reports_to_FNAME As String
Dim Subject2 As String
Dim From As String
Dim CountOfID As String
Dim Email_text As String
Dim Email_text1 As String
Dim db As DAO.Database
Dim rstTEST As DAO.Recordset
Dim rstData As DAO.Recordset
Dim tablename As String
'Dim SampleEmails As DAO.Recordset
Dim rstqry_Direct_Reports As DAO.Recordset
Dim ML_SUPV_EMPLID As Object
Dim Bob As Object
Set db = CurrentDb()
'Set rstData = db.OpenRecordset("Data")
'Set rstSampleEmails = db.OpenRecordset("SampleEm ails")
Set rstqry_Direct_Reports = db.OpenRecordset("qry_Dire ct_Reports ")
Set session = CreateObject("Notes.NotesS ession")
UserName = session.UserName
MsgBox (UserName)
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = session.GETDATABASE("", MailDbName)
If Maildb.ISOPEN = True Then
Else
Maildb.OPENMAIL
End If
rstqry_Direct_Reports.Move First
Set Bob = [rstqry_Direct_Reports]![M L_SUPV_EMP LID]
For Each Bob In rstqry_Direct_Reports
'Do While Not rstqry_Direct_Reports.EOF
Eml2 = rstqry_Direct_Reports![Eml ]
'Subject2 = [Forms]![EmailSample]![Tex t2]
'Email_text = "Dear " & rstqry_Direct_Reports![Fir st] & "," & Chr(13) & Chr(13)
'Email_text1 = Email_text & [Forms]![EmailSample]![Ema ilText1] & Chr(13) & Chr(13)
Email_text2 = "Below are the employees and non-employees for" & " " & [rstqry_Direct_Reports]![M L_REPORTS_ TO_NAME] & " " & "as of" & " " & [rstqry_Direct_Reports]![T oday] & Chr(13) & Chr(13)
Email_text3 = Email_text3 & Chr(13) & [rstqry_Direct_Reports]![E MPLID] & " " & [rstqry_Direct_Reports]![N ame] & " " & [rstqry_Direct_Reports]![E mployee Type]
Email_text4 = Email_text2 & Email_text3
'& "Username: " & rstTEST![UserName] & Chr(13) & Chr(13) & "Password: " & rstTEST![Password]
'From2 = [Forms]![EmailSample]![Fro m]
On Error GoTo errorhandler1
rstqry_Direct_Reports.Move Next
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
MailDoc.sendto = Eml2
MailDoc.Principal = "DatabasesRUs"
MailDoc.Subject = "Works"
MailDoc.Body = Email_text4
MailDoc.SaveMessageOnSend = True
MailDoc.PostedDate = Now()
'Loop
Next
MailDoc.Send 0, Recipient
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set session = Nothing
Set EmbedObj1 = Nothing
errorhandler1:
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set session = Nothing
Set EmbedObj1 = Nothing
MsgBox ("Email Sent")
End Function
Function Email()
Dim UserName As String
Dim MailDbName As String
Dim Maildb As Object
Dim MailDoc As Object
Dim AttachME As Object
Dim session As Object
Dim EmbedObj1 As Object
Dim Eml2 As String
Dim Reports_To_Name As String
Dim Recp As String
Dim Reports_to_FNAME As String
Dim Subject2 As String
Dim From As String
Dim CountOfID As String
Dim Email_text As String
Dim Email_text1 As String
Dim db As DAO.Database
Dim rstTEST As DAO.Recordset
Dim rstData As DAO.Recordset
Dim tablename As String
'Dim SampleEmails As DAO.Recordset
Dim rstqry_Direct_Reports As DAO.Recordset
Dim ML_SUPV_EMPLID As Object
Dim Bob As Object
Set db = CurrentDb()
'Set rstData = db.OpenRecordset("Data")
'Set rstSampleEmails = db.OpenRecordset("SampleEm
Set rstqry_Direct_Reports = db.OpenRecordset("qry_Dire
Set session = CreateObject("Notes.NotesS
UserName = session.UserName
MsgBox (UserName)
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = session.GETDATABASE("", MailDbName)
If Maildb.ISOPEN = True Then
Else
Maildb.OPENMAIL
End If
rstqry_Direct_Reports.Move
Set Bob = [rstqry_Direct_Reports]![M
For Each Bob In rstqry_Direct_Reports
'Do While Not rstqry_Direct_Reports.EOF
Eml2 = rstqry_Direct_Reports![Eml
'Subject2 = [Forms]![EmailSample]![Tex
'Email_text = "Dear " & rstqry_Direct_Reports![Fir
'Email_text1 = Email_text & [Forms]![EmailSample]![Ema
Email_text2 = "Below are the employees and non-employees for" & " " & [rstqry_Direct_Reports]![M
Email_text3 = Email_text3 & Chr(13) & [rstqry_Direct_Reports]![E
Email_text4 = Email_text2 & Email_text3
'& "Username: " & rstTEST![UserName] & Chr(13) & Chr(13) & "Password: " & rstTEST![Password]
'From2 = [Forms]![EmailSample]![Fro
On Error GoTo errorhandler1
rstqry_Direct_Reports.Move
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
MailDoc.sendto = Eml2
MailDoc.Principal = "DatabasesRUs"
MailDoc.Subject = "Works"
MailDoc.Body = Email_text4
MailDoc.SaveMessageOnSend = True
MailDoc.PostedDate = Now()
'Loop
Next
MailDoc.Send 0, Recipient
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set session = Nothing
Set EmbedObj1 = Nothing
errorhandler1:
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set session = Nothing
Set EmbedObj1 = Nothing
MsgBox ("Email Sent")
End Function
I doubt whether it's possible to loop over a RecordSet that way, using a For Each. I think a recordset isn't enumerable, see also http://msdn.microsoft.com/en-us/library/5ebk1751.aspx
Definently not. For Each loop will not work. You can use For i = 1 to rstqry_Direct_Reports.Coun t instead.
Leon
Leon
ASKER
leonstryker,
would you be able to modify my code to incorporate the i = 1?
would you be able to modify my code to incorporate the i = 1?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't see much difference with the original Do-While loop... You could change the Do-While-Not in Do-Until, but that's all.
By the way, a Do-loop is probably even faster than the indexed way Leon suggested.
By the way, a Do-loop is probably even faster than the indexed way Leon suggested.
ASKER
any chance you could post a code sample sjef bosman?
ASKER
in the recordset, is there a way to have the loop send a new email whenever the ML_SUPV_EMPLID changes (with the resulting records)?
ASKER
no