Link to home
Start Free TrialLog in
Avatar of shawngert
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("SampleEmails")
Set rstqry_Direct_Reports = db.OpenRecordset("qry_Direct_Reports")
Set session = CreateObject("Notes.NotesSession")
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.MoveFirst
Set Bob = [rstqry_Direct_Reports]![ML_SUPV_EMPLID]
For Each Bob In rstqry_Direct_Reports
'Do While Not rstqry_Direct_Reports.EOF
Eml2 = rstqry_Direct_Reports![Eml]
'Subject2 = [Forms]![EmailSample]![Text2]
'Email_text = "Dear " & rstqry_Direct_Reports![First] & "," & Chr(13) & Chr(13)
'Email_text1 = Email_text & [Forms]![EmailSample]![EmailText1] & Chr(13) & Chr(13)
Email_text2 = "Below are the employees and non-employees for" & " " & [rstqry_Direct_Reports]![ML_REPORTS_TO_NAME] & " " & "as of" & " " & [rstqry_Direct_Reports]![Today] & Chr(13) & Chr(13)
Email_text3 = Email_text3 & Chr(13) & [rstqry_Direct_Reports]![EMPLID] & "     " & [rstqry_Direct_Reports]![Name] & "     " & [rstqry_Direct_Reports]![Employee Type]
Email_text4 = Email_text2 & Email_text3

'& "Username: " & rstTEST![UserName] & Chr(13) & Chr(13) & "Password: " & rstTEST![Password]
'From2 = [Forms]![EmailSample]![From]
On Error GoTo errorhandler1
rstqry_Direct_Reports.MoveNext
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

Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

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.Count instead.

Leon
Avatar of shawngert
shawngert

ASKER

leonstryker,

would you be able to modify my code to incorporate the i = 1?
ASKER CERTIFIED SOLUTION
Avatar of leonstryker
leonstryker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
any chance you could post a code sample sjef bosman?
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)?
no