Paging SZLAMANY and others! I wish to convert the following code but I don't know where to start...
VB Code:
  1. 'Function:
  2.     'To add values to Expenditure field wherein DateEarned field has elapsed for a month,
  3.     'the expenditure value will be deducted from the TotalCost which should be deducted
  4.     'from the Earnings table to reflect the real income for the previous months
  5. Private Sub Command2_Click()
  6.     Dim adoRecordset    As ADODB.Recordset
  7.     Dim adoRecordset2   As ADODB.Recordset
  8.     Dim Total           As Currency
  9.     Dim TotalFrameCost  As Currency
  10.     Dim curExpenditure  As Currency
  11.     Dim curEyeglass     As Currency
  12.     Dim strData         As String
  13.     Dim strSQL          As String
  14.     Dim PurchaseCode    As Long
  15.    
  16.     'Clear previous automated calculations:
  17.     strSQL = "UPDATE tabEarningRecords " & _
  18.              "SET Expenditure = 0 " & _
  19.              "WHERE (Sequence IN " & _
  20.              "(SELECT tabEarningRecords.Sequence " & _
  21.              "FROM tabEGPurchase INNER JOIN " & _
  22.              "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
  23.              "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence " & _
  24.              "WHERE tabEGPurchase.Code = " & PurchaseCode & "))"
  25.     If ExecuteSQL(strSQL) = True Then
  26.         MsgBox "Ok!"
  27.     Else
  28.         MsgBox "Not Ok!"
  29.     End If
  30.    
  31.     strSQL = "SELECT tabEGPurchase.Code " & _
  32.              "FROM tabEGPurchase INNER JOIN " & _
  33.              "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
  34.              "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
  35.              "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
  36.              "WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) " & _
  37.              "GROUP BY tabEGPurchase.Code"
  38.     Set adoRecordset2 = New ADODB.Recordset
  39.     With adoRecordset2
  40.         .Open strSQL, adoConn, adOpenForwardOnly, adLockReadOnly
  41.         Do While Not .EOF
  42.             'Get purchase code:
  43.             PurchaseCode = .Fields(0)
  44.            
  45.             'Get total cost:
  46.             strSQL = "FROM tabEGPurchase LEFT OUTER JOIN " & _
  47.                      "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
  48.                      "WHERE tabEGPurchase.Code = " & PurchaseCode & ""
  49.             TotalFrameCost = DesiredField("ISNULL(tabFrameList.BuyingPrice, 0) + ISNULL(tabEGPurchase.MTC, 0)", strSQL)
  50.             MsgBox TotalFrameCost, , "TotalCost"
  51.            
  52.             strSQL = "SELECT  tabEarningRecords.EyeGlass, tabEarningRecords.Expenditure " & _
  53.                      "FROM tabEGPurchase INNER JOIN " & _
  54.                      "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
  55.                      "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
  56.                      "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
  57.                      "WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) AND (tabEGPurchase.Code = " & PurchaseCode & ")"
  58.             Set adoRecordset = New ADODB.Recordset
  59.             Total = TotalFrameCost
  60.            
  61.             With adoRecordset
  62.                 .Open strSQL, adoConn, adOpenForwardOnly, adLockOptimistic
  63.                 Do While Not .EOF
  64.                     curEyeglass = .Fields("Eyeglass")
  65.                     If Total >= curEyeglass Then
  66.                         Total = Total - curEyeglass
  67.                         curExpenditure = curEyeglass
  68.                     Else
  69.                         curExpenditure = Total
  70.                         Total = 0
  71.                     End If
  72.                     .Fields("Expenditure") = curExpenditure
  73.                     .Update
  74.                     strData = "Eyeglass: " & curEyeglass
  75.                     strData = strData & vbNewLine & "Expenditure: " & curExpenditure
  76.                     strData = strData & vbNewLine & "Remaining: " & Total
  77.                     MsgBox strData
  78.                     If Total = 0 Then
  79.                         Exit Do
  80.                     End If
  81.                     .MoveNext
  82.                 Loop
  83.                 .Close
  84.             End With
  85.             .MoveNext
  86.         Loop
  87.         .Close
  88.     End With
  89.     Set adoRecordset = Nothing
  90.     Set adoRecordset2 = Nothing
  91. End Sub