I have a small set of code which I just added a nested loop. Basically the program will loop through multiple selections and get multiple records for each selection (the nested part). I then need it to send an email for each record in the nested loop. I can't seem to get it! Any ideas? I tried to note the nested part below. This is something simple but my brain is fried! Here is the code:
Private Sub cmdProcessInvCat_Click()
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim fs As Object
Dim strFrom As String, strTo As String, strFilename As String, strE As String, strYN As String
Dim txtDate As String, FileName As String, ClientNumber As String
Dim intCount As Integer, XONum As String, Message As String, strSQL As String
txtDate = Me.txtEOMDate.Value
Set db = CurrentDb
Set rs = db.OpenRecordset("tblFileL
ist")
Set fs = CreateObject("Scripting.Fi
leSystemOb
ject")
intCount = 1
For intCount = 1 To rs.RecordCount
strFilename = rs.Fields(0) 'filename
strFrom = rs.Fields(1) 'source directory
strTo = rs.Fields(2) 'destination location
strYN = rs.Fields(3) 'checkbox
DoCmd.SetWarnings False
If strYN = "True" Then
FileName = txtDate & Left(strFilename, 10)
ClientNumber = Right(FileName, 4)
XONum = Mid(strFilename, 3, 4)
If ClientNumber = "C156" Then
strSQL = "Delete * from tblXOOrigC156"
DoCmd.RunSQL (strSQL)
'TEMP DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblXOOrigC156", strFrom & txtDate & "-" & strFilename, True, ""
'Call Macro
'TEMP DoCmd.RunMacro "mcrProcessC156"
'Copy to new location
'TEMP DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"tblXOC156", strTo & txtDate & "_" & ClientNumber & "_XO" & XONum
'Delete Table
'TEMP DoCmd.DeleteObject acTable, "tblXOC156"
GoTo EndSub
End If
If ClientNumber = "C150" Then
strSQL = "Delete * from tblXOOrigC150"
DoCmd.RunSQL (strSQL)
'Import
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblXOOrigC150", strFrom & txtDate & "-" & strFilename, True, ""
'Call Macro
DoCmd.RunMacro "mcrProcessC150"
'Copy to new location
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"tblXOC150", strTo & txtDate & "_" & ClientNumber & "_XO" & XONum
'Delete Table
DoCmd.DeleteObject acTable, "tblXOC150"
GoTo EndSub
End If
If ClientNumber = "C908" Then
strSQL = "Delete * from tblXOOrigC908"
DoCmd.RunSQL (strSQL)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblXOOrigC908", strFrom & txtDate & "-" & strFilename, True, ""
'Call Macro
DoCmd.RunMacro "mcrProcessC908"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"tblXOC908", strTo & txtDate & "_" & ClientNumber & "_XO" & XONum
'Delete Table
DoCmd.DeleteObject acTable, "tblXOC908"
GoTo EndSub
End If
End If
EndSub:
MsgBox (XONum & "," & ClientNumber)
Dim RS_Query As String, rs2 As DAO.Recordset, InvNumber As String, InvName As String
Dim ReportLocation As String, email As String, FirstName As String, intCount2 As Integer
Dim Investor As String, SmallClientNumber As String
SmallClientNumber = Right(ClientNumber, 3)
RS_Query = "SELECT tblInvList.InvNumber, tblInvList.InvName, " & _
"tblInvList.ReportLocation
, tblAssociates.[First Name], tblAssociates.Email " & _
"FROM tblAssociates INNER JOIN tblInvList ON tblAssociates.ID = tblInvList.AssociateID " & _
"WHERE tblInvList.ClientNo='" & SmallClientNumber & "' AND " & _
"tblInvList.XONumber ='" & XONum & "'"
Set rs2 = db.OpenRecordset(RS_Query)
PROBLEMS WITH THE NESTED FOR LOOP BELOW!!!
intCount2 = 1
For intCount2 = 1 To rs2.RecordCount
InvNumber = Nz(rs2("InvNumber"), "")
InvName = Nz(rs2("InvName"), "")
ReportLocation = Nz(rs2("ReportLocation"), "")
email = Nz(rs2("Email"), "")
FirstName = Nz(rs2("First Name"), "")
email = "Bob@Tom.com"
strsubject = "Test"
strMessage = "Test"
rs2.MoveNext
rs2.MoveNext
Next
END NESTED FOR LOOP!!!
rs.MoveNext
Next
DoCmd.SetWarnings True
MsgBox ("Process Complete.")
EndProgram:
End Sub
Start Free Trial