I am trying to loop through one query (qryMailingList) and using the value in field UserID. I am using this value in my criteria in another query (qryExceptions field:Opportunity Primary Login).
If qryExeptions has records in it, I want to email this to the user using the email address in the field TestEmail
If qryExceptions opens with no records I want to go to the next record in qryMailingList and do the process again until I reach the end of the file.
I am a novice at VB so am struggling a bit. I can't get the bit to work where if there are no records then go to the next record.
The code I have so far is (which doesn't seem to work):
Public Function User()
User = MyUser
End Function
Sub Output_Query()
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim MailRS As Recordset
Dim ExcRS As Recordset
Dim MyUser As String
Dim MyExc As String
Set MailRS = CurrentDb.OpenRecordset("qryMailingList", dbOpenDynaset)
Set ExcRS = CurrentDb.OpenRecordset("qryExceptions", dbOpenDynaset)
MailRS.MoveFirst
Do Until MailRS.EOF = True
MyUser = MailRS("User ID")
DoCmd.OpenQuery ("qryExceptions")
MyExc = ExcRS("Opportunity Primary Login")
If ExcRS <> Null Then
DoCmd.TransferSpreadsheet
Else
MailRS.MoveNext
End If
Loop
MailRS.Close
End Sub
Any help would be very much appreciated.
If qryExeptions has records in it, I want to email this to the user using the email address in the field TestEmail
If qryExceptions opens with no records I want to go to the next record in qryMailingList and do the process again until I reach the end of the file.
I am a novice at VB so am struggling a bit. I can't get the bit to work where if there are no records then go to the next record.
The code I have so far is (which doesn't seem to work):
Public Function User()
User = MyUser
End Function
Sub Output_Query()
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim MailRS As Recordset
Dim ExcRS As Recordset
Dim MyUser As String
Dim MyExc As String
Set MailRS = CurrentDb.OpenRecordset("qryMailingList", dbOpenDynaset)
Set ExcRS = CurrentDb.OpenRecordset("qryExceptions", dbOpenDynaset)
MailRS.MoveFirst
Do Until MailRS.EOF = True
MyUser = MailRS("User ID")
DoCmd.OpenQuery ("qryExceptions")
MyExc = ExcRS("Opportunity Primary Login")
If ExcRS <> Null Then
DoCmd.TransferSpreadsheet
Else
MailRS.MoveNext
End If
Loop
MailRS.Close
End Sub
Any help would be very much appreciated.