Saving record in VB2010

Posted on 2011-03-18
Last Modified: 2012-08-13

I have a backend database as my bindingsource, and am attempting to use a Save command button to reflect the changes on my form into my database.  Please advise.

Thank you.

Option Explicit On

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataGridView

Public Class frmInput
    Dim sqlString As String = "SELECT * FROM tblInsuranceClaim"
    Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Administrator\Desktop\MyDBBackend.mdb;User ID=admin"
    Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
    Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlString, OleDBConn1)
    Dim dbGen As New OleDbCommandBuilder(OleDbDataAdapter1)

    Private Sub frmOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDBBackendDataSet3.tblInsuranceClaim' table. You can move, or remove it, as needed.
        txtPDrug.Text = frmDrugs.txtDrugHidden.Text

            OleDbDataAdapter1.AcceptChangesDuringUpdate = True
        Catch ex As Exception
            MsgBox("An exception was thrown because the back-end database is currently not allowing updates. Please try again.")
        End Try

        Catch ex As Exception
            MsgBox("An exception was thrown because a connection to the database or database tables cannot be established")
        End Try

    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
            OleDbDataAdapter1.Update(MyDBBackendDataSet, "tblInsuranceClaim")
            MsgBox("Update Successful")
        Catch ex As Exception
            MsgBox("Update Failed")
        End Try
    End Sub

Open in new window

Question by:--TripWire--
  • 3
  • 2

Assisted Solution

Alex_W earned 200 total points
ID: 35167171
Consider My.Settings to save settings between runtime sessions.  You'll find it in the project properties under the Settings page.

To save you just need something like:

My.Settings.CommandButton = txtCommand.Text

To load you you use the forms load event with:

txtCommand.Text = My.Settings.CommandButton.

Author Comment

ID: 35167248
Thanks for the reply - but I think you misunderstood my question.
My form has a number of different controls on it, mostly textboxes.  And I want the information that the user types in to be saved into the database when the user clicks the Save command button on the form.

Accepted Solution

Alex_W earned 200 total points
ID: 35167527
Ah my mistake, to update changes in your binding source controls you'll want to use the data adapters update method.  I see your using an Access DB, here is some specifics:

Author Comment

ID: 35167700
hmm...Well, I already have the adapter.update method in my code above

Author Closing Comment

ID: 37595419
It was helpful, but I wasn't able to find a full solution this way.  But thank you.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now