none
Save data on database when exit

    Question

  • Hello ! 

    I am working on a program, where i can organize my movies, and i have a database on acess, integrated with visual basic. And i want that when i close the program, it saves the data ( in case i forget to click the save button ).

    I have already made a button ( an exit button ) that, when you click it, it saves all the data and then close. But virtually i use this button. I ever use the original close button, and i dont know how to add this comand on this original button. 

    Thanks for the help !!

    Sunday, January 26, 2014 10:58 PM

Answers

  • Hello,

    I would suggest doing this in FormClosing event i.e. the save button and the close button call the lines in SaveChanges to save changes to the database table.

    Public Class Form1
        Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
        Handles CustomerBindingNavigatorSaveItem.Click
            SaveChanges()
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            SaveChanges()
        End Sub
        Private Sub SaveChanges()
            Me.Validate()
            Me.CustomerBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.CustomerDataSet1)
        End Sub

    Or check for changes

    Public Class Form1
        Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
        Handles CustomerBindingNavigatorSaveItem.Click
            SaveChanges()
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If CustomerDataSet1.HasChanges Then
                SaveChanges()
            End If
        End Sub
        Private Sub SaveChanges()
            Me.Validate()
            Me.CustomerBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.CustomerDataSet1)
        End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Sunday, January 26, 2014 11:29 PM

All replies

  • Hello,

    I would suggest doing this in FormClosing event i.e. the save button and the close button call the lines in SaveChanges to save changes to the database table.

    Public Class Form1
        Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
        Handles CustomerBindingNavigatorSaveItem.Click
            SaveChanges()
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            SaveChanges()
        End Sub
        Private Sub SaveChanges()
            Me.Validate()
            Me.CustomerBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.CustomerDataSet1)
        End Sub

    Or check for changes

    Public Class Form1
        Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
        Handles CustomerBindingNavigatorSaveItem.Click
            SaveChanges()
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If CustomerDataSet1.HasChanges Then
                SaveChanges()
            End If
        End Sub
        Private Sub SaveChanges()
            Me.Validate()
            Me.CustomerBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.CustomerDataSet1)
        End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Sunday, January 26, 2014 11:29 PM
  • Thats what i want. Now i can forget to save, without problems.

    If i get some troubles, i ask on this post again.

    Thanks for the attention and for replying. All the best !!!!! 


    Sunday, January 26, 2014 11:40 PM