I am trying to calculate the 'Profit' field based on the current record's 'ProjectTotalBillingEstimate' field and the 'ProjectActualCost' field. I want the 'Profit' text field to be calculated every time a record is changed, either via the navigation toolbar or the Project ID drop down box. I tried performing the calculation everytime the 'Product ID' changed but then the values in the fields of the previous record were used. I was wondering if there was an event that I could use the would occur only after the entire contents of a record were loaded.
(Image Attached)
Essentially what I need to happen is when one moves to another project record the profit field needs to be calculated only after all the fields update to the current record.
'Profit' calculation = 'ProjectTotalBillingEstimate' - 'ProjectActualCost'
I tried this,
Private Sub txtProjectID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProjectID.TextChanged
txtProfit.Text = CStr(FormatCurrency((CDbl(txtProjectTotalBillingEstimate.Text) - CDbl(txtProjectActualCost.Text))))
End Sub
but the event occurrence was 'Project ID' changes > CalcProfit() runs using previous record's fields > the the remaining fields update to current records. Since the function runs before the remaining records change the 'Profit' calculation is wrong for the current record.
Another thing that I came across is something called OnCurrent, but for the life of me I can't figure out how it is used or implemented. I searched forums and saw that people kept saying to use OnCurrent but I could not find a clear example as to how to use it.
Here is my code:
Public Class frmMain
Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
Me.Validate()
Me.ProjectsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ProjectsDataSet)
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ProjectsDataSet.Projects' table. You can move, or remove it, as needed.
Me.ProjectsTableAdapter.Fill(Me.ProjectsDataSet.Projects)
CalcProfit()
End Sub
Function CalcProfit()
If (txtProjectTotalBillingEstimate.Text.Length > 0 And txtProjectActualCost.Text.Length > 0) Then
txtProfit.Text = CStr(FormatCurrency((CDbl(txtProjectTotalBillingEstimate.Text) - CDbl(txtProjectActualCost.Text))))
End If
End Function
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class