Detecting user changes to data on form

I have a form which is used to make changes to a database.

When the form first opens the form fields are populated from the database. How can I tell if the user changes any of the fields. I would like to be able to detect this so that if the user tries to close the form I can prompt them to see if they wish to save the changes.

Thanks


Answer this question

Detecting user changes to data on form

  • Robert Seiler

    Hi Carl,

    you can use the HasChanges method of the DataSet or the GetChanges method of the DataTable objects.

    For example:

    If (myDataSet.HasChanges) Then
        ' Prompt user here
    End If

    Or for a typed dataset you can do

    If (myDataSet.MyTable.GetChanges() IsNot Nothing) Then
        ' Prompt user here
    End If

    After you prompt the user, if the user answers 'yes' to save the changes, you can use the DataAdapters to send the changes back to the database.

    HTH

    Antoine
    Visual Basic team


  • kramer

    Yes, that tells the binding manager to make sure to push current edits to the underlying dataset.

    Antoine
    Visual Basic team

  • israfel

    I had to add an EndEdit() as below to get it to work.  Is that the right thing to do Individual is the table.

    Me
    .IndividualBindingSource.EndEdit()

    If Me.VBNMDATADataSet.Individual.GetChanges() IsNot Nothing Then MessageBox.Show("Changes")

    GS


  • Detecting user changes to data on form