Function Run_Fundings_Routine()
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim RS2 As DAO.Recordset
Dim RS3 As DAO.Recordset
Dim SQL As String
Dim SQL2 As String
Dim SQL3 As String
Set DB = CurrentDb()
'Delete old Notice_Temp table
DoCmd.SetWarnings False
SQL = "DELETE * " & _
"FROM Notice_Temp "
DoCmd.RunSQL (SQL)
'Find last run date
SQL = "SELECT TOP 1 Run_Days.Run_Date " & _
"FROM Run_Days " & _
"ORDER BY Run_Days.Run_Date DESC;"
Set RS = DB.OpenRecordset(SQL)
RS.MoveFirst
Last_Update = RS![Run_Date]
RS.Close
'Days since last run
Days_Since_Last_Run = Date - Last_Update
'Go through large secondary table to find fundings >= 30 days
SQL = "SELECT dbo_vw_SecondaryMarketing_AllLoans.[Loan Number], dbo_vw_SecondaryMarketing_AllLoans.[Borrower Last Name], dbo_vw_SecondaryMarketing_AllLoans.Branch, dbo_vw_SecondaryMarketing_AllLoans.LoanFolder, dbo_vw_SecondaryMarketing_AllLoans.[Funding Funds Sent Date], dbo_vw_SecondaryMarketing_AllLoans.Investor, Date()-[Funding Funds Sent Date] AS Funding_Maturity " & _
"FROM dbo_vw_SecondaryMarketing_AllLoans " & _
"WHERE (((dbo_vw_SecondaryMarketing_AllLoans.LoanFolder) Not Like '*Test*' And " & _
"(dbo_vw_SecondaryMarketing_AllLoans.LoanFolder) Not Like '*Train*' And (dbo_vw_SecondaryMarketing_AllLoans.LoanFolder) Not Like '*Trash*') AND ((dbo_vw_SecondaryMarketing_AllLoans.[Funding Funds Sent Date]) Is Not Null) AND ((dbo_vw_SecondaryMarketing_AllLoans.Investor) Not Like '*Housing*' And (dbo_vw_SecondaryMarketing_AllLoans.Investor)<>'WHEDA' And (dbo_vw_SecondaryMarketing_AllLoans.Investor) Not Like 'U.S*' And (dbo_vw_SecondaryMarketing_AllLoans.Investor) Not Like 'TDHC*' And (dbo_vw_SecondaryMarketing_AllLoans.Investor) Not Like 'PIMA*' And (dbo_vw_SecondaryMarketing_AllLoans.Investor)<>'NCHFA' And (dbo_vw_SecondaryMarketing_AllLoans.Investor)<>'Portfolio' And (dbo_vw_SecondaryMarketing_AllLoans.Investor)<>'*FIMC' And (dbo_vw_SecondaryMarketing_AllLoans.Investor)<>'Fairway Independent Mortgage Corporation') AND ((Date()-[Funding Funds Sent Date])>=30) AND ((dbo_vw_SecondaryMarketing_AllLoans.[Purchase Advice Date]) Is Null)) " & _
"ORDER BY Date()-[Funding Funds Sent Date] DESC;"
Set RS = DB.OpenRecordset(SQL)
RS.MoveFirst
Do Until RS.EOF = True
MyLoan = RS![Loan Number]
Fund_Mat = RS![Funding_Maturity]
Branch = RS![Branch]
Investor = RS![Investor]
Last_Name = RS![Borrower Last Name]
'see if the number of days since funded will warrant a notice
'For iter = Days_Since_Last_Run - 1 To 0 Step -1
For iter = 0 To Days_Since_Last_Run - 1
SQL2 = "SELECT Notice_Days.Notice_Days " & _
"FROM Notice_Days " & _
"WHERE Notice_Days.Notice_Days =" & Fund_Mat - iter & ";"
Set RS2 = DB.OpenRecordset(SQL2)
If RS2.RecordCount <> 0 Then
'It warrants a notice. Now see if record for this loan exists in Notice_Tracking.
SQL3 = "SELECT Notice_Tracking.Loan, Notice_Tracking.Branch, Notice_Tracking.[Last_notice_sent_for_(days)], Notice_Tracking.Date_Last_Notice " & _
"FROM Notice_Tracking " & _
"WHERE (((Notice_Tracking.Loan)='" & MyLoan & "'));"
Set RS3 = DB.OpenRecordset(SQL3)
'Record exists in Notice_Tracking, so update info
If RS3.RecordCount <> 0 Then
RS3.Edit
RS3![Last_notice_sent_for_(days)] = Fund_Mat - iter
RS3![Date_Last_Notice] = Date
RS3.Update
'Record does not exist in Notice_Tracking, so add info
Else
SQL2 = "INSERT INTO Notice_Tracking ( Loan, Borrower_Last_Name, Branch, Investor, [Last_notice_sent_for_(days)], Date_Last_Notice ) " & _
"SELECT " & MyLoan & ", '" & Replace(Last_Name, "'", "''") & "', " & Branch & ", '" & Replace(Investor, "'", "''") & "', " & Fund_Mat - iter & ", #" & Date & "#;"
DoCmd.RunSQL (SQL2)
End If
'Add info to Notice_Temp
SQL2 = "INSERT INTO Notice_Temp ( Loan, Borrower_Last_Name, Branch, Investor, Days ) " & _
"SELECT " & MyLoan & ", '" & Replace(Last_Name, "'", "''") & "', " & Branch & ", '" & Replace(Investor, "'", "''") & "', " & Fund_Mat - iter & ";"
DoCmd.RunSQL (SQL2)
RS2.Close
RS3.Close
GoTo Next_Loan_Plz
End If
Next iter
Next_Loan_Plz:
RS.MoveNext
Loop
'update last_run date
SQL3 = "INSERT INTO Run_Days ( Run_Date ) " & _
"SELECT #" & Date & "# AS Run_Date;"
DoCmd.RunSQL (SQL3)
DoCmd.SetWarnings True
RS.Close
End Function