'Function:
'To add values to Expenditure field wherein DateEarned field has elapsed for a month,
'the expenditure value will be deducted from the TotalCost which should be deducted
'from the Earnings table to reflect the real income for the previous months
Private Sub Command2_Click()
Dim adoRecordset As ADODB.Recordset
Dim adoRecordset2 As ADODB.Recordset
Dim Total As Currency
Dim TotalFrameCost As Currency
Dim curExpenditure As Currency
Dim curEyeglass As Currency
Dim strData As String
Dim strSQL As String
Dim PurchaseCode As Long
'Clear previous automated calculations:
strSQL = "UPDATE tabEarningRecords " & _
"SET Expenditure = 0 " & _
"WHERE (Sequence IN " & _
"(SELECT tabEarningRecords.Sequence " & _
"FROM tabEGPurchase INNER JOIN " & _
"tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
"tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence " & _
"WHERE tabEGPurchase.Code = " & PurchaseCode & "))"
If ExecuteSQL(strSQL) = True Then
MsgBox "Ok!"
Else
MsgBox "Not Ok!"
End If
strSQL = "SELECT tabEGPurchase.Code " & _
"FROM tabEGPurchase INNER JOIN " & _
"tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
"tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
"tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
"WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) " & _
"GROUP BY tabEGPurchase.Code"
Set adoRecordset2 = New ADODB.Recordset
With adoRecordset2
.Open strSQL, adoConn, adOpenForwardOnly, adLockReadOnly
Do While Not .EOF
'Get purchase code:
PurchaseCode = .Fields(0)
'Get total cost:
strSQL = "FROM tabEGPurchase LEFT OUTER JOIN " & _
"tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
"WHERE tabEGPurchase.Code = " & PurchaseCode & ""
TotalFrameCost = DesiredField("ISNULL(tabFrameList.BuyingPrice, 0) + ISNULL(tabEGPurchase.MTC, 0)", strSQL)
MsgBox TotalFrameCost, , "TotalCost"
strSQL = "SELECT tabEarningRecords.EyeGlass, tabEarningRecords.Expenditure " & _
"FROM tabEGPurchase INNER JOIN " & _
"tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
"tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
"tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
"WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) AND (tabEGPurchase.Code = " & PurchaseCode & ")"
Set adoRecordset = New ADODB.Recordset
Total = TotalFrameCost
With adoRecordset
.Open strSQL, adoConn, adOpenForwardOnly, adLockOptimistic
Do While Not .EOF
curEyeglass = .Fields("Eyeglass")
If Total >= curEyeglass Then
Total = Total - curEyeglass
curExpenditure = curEyeglass
Else
curExpenditure = Total
Total = 0
End If
.Fields("Expenditure") = curExpenditure
.Update
strData = "Eyeglass: " & curEyeglass
strData = strData & vbNewLine & "Expenditure: " & curExpenditure
strData = strData & vbNewLine & "Remaining: " & Total
MsgBox strData
If Total = 0 Then
Exit Do
End If
.MoveNext
Loop
.Close
End With
.MoveNext
Loop
.Close
End With
Set adoRecordset = Nothing
Set adoRecordset2 = Nothing
End Sub