Public Function GenerateAUGUST() As Boolean
' Return a complete RecordSet containing the selected Fiscal Year's List of Requisitions Captured on BuildSmart
Dim DB As New ADODB.Connection
DB.Open "Provider=SQLNCLI10.1;User ID=***;Password=***;Persist Security Info=False;Data Source=***;Database=BSBS_BrolazNew2"
Dim AugReqs As New ADODB.Recordset
Dim RowStart As Integer
Dim RowNum As Integer ' Current iterated Row Number, Dynamic on RowCount of Recordset
Dim Counter As Integer
AugReqs.Open "SELECT [CREATEDATE] as [Requisition Date], [REQID] as [Req Code], [REQNUMBER] as [Requisition Number], _
[REQSUBJECT] as [Requisition Subject], [SHORTDESCR] as [Requisition Description], (select sum(PRICE*QTY) from _
[BSBS_BrolazNew2].[dbo].[REQITEMS] WHERE REQID = dbo.REQ.REQID) as [Requisition Total], (select CONTRNUMBER from _
CONTRACTS WHERE CONTRID = (select DISTINCT TOP 1 [CONTRACTID] from [BSBS_BrolazNew2].[dbo].[REQITEMS] WHERE REQID _
= dbo.REQ.REQID)) as [Contract] FROM [BSBS_BrolazNew2].[dbo].[REQ] WHERE CREATEDATE >= '2012-08-01' AND CREATEDATE _
<= '2012-08-31' AND (select TOP 1 ALLOCATION from REQITEMS WHERE REQID = [dbo].[REQ].[REQID]) = 'Contracts' AND _
(select sum(PRICE*QTY) as [Requisition Total] from [BSBS_BrolazNew2].[dbo].[REQITEMS] WHERE REQID = dbo.REQ.REQID) > 0 _
ORDER BY [Contract]", DB, adOpenDynamic, adLockOptimistic
' Type out this Result Set onto the AUGUST Report Sheet
RowStart = 4
RowNum = RowStart
Counter = 0
' Clear Cell Contents
Sheet4.Range("A4", "J900").ClearContents
With AugReqs
.MoveFirst
While Not .EOF
Sheet4.Cells(RowNum, "A") = Format(.Fields("Requisition Date").Value, "dd/MM/yyyy")
Sheet4.Cells(RowNum, "B") = .Fields("Req Code").Value
Sheet4.Cells(RowNum, "C") = .Fields("Requisition Number").Value
Sheet4.Cells(RowNum, "D") = .Fields("Requisition Subject").Value
Sheet4.Cells(RowNum, "E") = .Fields("Requisition Description").Value
Sheet4.Cells(RowNum, "F") = Module1.GetBSContractID(.Fields("Req Code").Value)
Sheet4.Cells(RowNum, "G") = .Fields("Requisition Total").Value
'Sheet4.Cells(RowNum, "H") = GetProjectBudget(Module1.GetBSContractID(.Fields("Req Code").Value)) _
'GetGLCodeBudget(GetLedgerCode(.Fields("Req Code").Value), Module1.GetBSContractID(.Fields("Req Code").Value))
Sheet4.Cells(RowNum, "I") = "=H" & RowNum & "-G" & RowNum
Sheet4.Cells(RowNum, "J") = "=IF(H" & RowNum & "<>0, I" & RowNum & "/H" & RowNum & ", 0)"
If .Fields("Requisition Subject").Value = "Subcontractor." Then
Sheet4.Cells(RowNum, "K") = .Fields("Requisition Total").Value
Else
Sheet4.Cells(RowNum, "K") = "0"
End If
RowNum = RowNum + 1
Counter = Counter + 1
.MoveNext
Wend
End With
Dim ProjectBudgetTotal As Double
Dim CurrProj As String
Dim ProjBud As Double
ProjectBudgetTotal = 0
Dim ProjectsInMonth As New ADODB.Recordset
ProjectsInMonth.Open "SELECT DISTINCT (select CONTRNUMBER from CONTRACTS WHERE CONTRID = (select DISTINCT TOP 1 _
[CONTRACTID] from [BSBS_BrolazNew2].[dbo].[REQITEMS] WHERE REQID = dbo.REQ.REQID)) as [Contract] FROM [BSBS_BrolazNew2].[dbo].[REQ] _
WHERE CREATEDATE >= '2012-08-01' AND CREATEDATE <= '2012-08-31' AND (select TOP 1 ALLOCATION from REQITEMS WHERE REQID = [dbo].[REQ].[REQID]) _
= 'Contracts' AND (select sum(PRICE*QTY) as [Requisition Total] from [BSBS_BrolazNew2].[dbo].[REQITEMS] WHERE REQID = dbo.REQ.REQID) > 0 _
AND ((select CONTRNUMBER from CONTRACTS WHERE CONTRID = (select DISTINCT TOP 1 [CONTRACTID] from [BSBS_BrolazNew2].[dbo].[REQITEMS] _
WHERE REQID = dbo.REQ.REQID))) IS NOT NULL ORDER BY [Contract]", DB, adOpenDynamic, adLockOptimistic
If Not (ProjectsInMonth.EOF = True) And Not (ProjectsInMonth.BOF = True) Then
ProjectsInMonth.MoveFirst
While Not ProjectsInMonth.EOF
CurrProj = ProjectsInMonth.Fields(0).Value
ProjBud = GetProjectBudget(CurrProj)
ProjectBudgetTotal = ProjectBudgetTotal + ProjBud
' Find this Project in the F Column
On Error Resume Next
Sheet4.Columns("F:F").Select
Sheet4.Range("F1").Activate
Selection.Find(What:=CurrProj, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ProjectBugetTotal = ProjectBudgetTotal + GetProjectBudget(CurrProj)
' Display the Project Budget in this First instance's H Column
Sheet4.Cells(ActiveCell.Row, "H") = ProjBud
ProjectsInMonth.MoveNext
Wend
End If
Sheet4.Cells(1, "H") = "=SUM(H4:H900)"
GlobalCounter = GlobalCounter + Counter
GenerateAUGUST = True
'MsgBox "The August Report for Fiscal Year: " & Sheet3.Cells(5, "E") & " has been successfully generated with " & Counter & " Requisitions processed for that Month.", vbInformation
End Function