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

from where you pickingg 'ProjectTotalBillingEstimate' and 'ProjectActualCost'
is these field are from database or u calculating it from database please let me know

from where you pickingg 'ProjectTotalBillingEstimate' and 'ProjectActualCost'
is these field are from database or u calculating it from database please let me know

Yes, these fields are in a database, but the 'Profit' field is not. All the fields on the form are from the database except for the 'Profit' field which does not get stored in the database at all.

then you can do one thing put command buttons for movement of records and after moving recordset then you can calculate proft it will 100% workout dont use ado component for database connectivity instead of that use coding base connection to database that will be better for doing this type of stuff.....

if not done than attach your project with database file ill do that for you

This is a vb.Net question. I have asked the mods to move it to vb.Net from here (VB4/5/6).:)

This is a vb.Net question. I have asked the mods to move it to vb.Net from here (VB4/5/6).:)

I did not know that. On the front of my textbook all it says is 'Visual Basic', so I assumed my post belonged in the other forum. No wonder I haven't been getting any good responses. Any chance you think you could help me out? Anybody?

It was a pleasure. Unfortunately I'm not well versed in .Net, so I'm afraid I will not be of much help.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.