I' sorry but i need some help, i cant find the error. I want to add a new record to the database (access 2010), but although the dataset is updated, the added information does not save do DB.
THe code bellow is the code that i run when press the confirm data, it is suposed to add the record to the data base after it finish running, but it doesnt? What am i doing wrong?

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

    Dim Nom As String
    Dim mat As Integer
    Dim post As String
    Dim ucc1 As Boolean
    Dim pbispo As Boolean
    Dim lingue As Boolean
    Dim mot As Boolean
    Dim cozi As Boolean
    Dim trc As Integer
    trc = 0
    mot = CheckBox3.Checked
    ucc1 = CheckBox4.Checked
    pbispo = CheckBox5.Checked
    lingue = CheckBox6.checked()
    cozi = CheckBox7.Checked
    Nom = Nome_militar.Text
    mat = Val(Matricula_Text.Text)
    post = TextBox5.Text
    Try
        Me.Validate()
        Me.UCCESCALASDataSet.Militares.NewMilitaresRow()
        Me.UCCESCALASDataSet.Militares.AddMilitaresRow(Nom, post, mat, False, cozi, ucc1, lingue, pbispo, mot, trc, False, False)
        Me.MilitaresTableAdapter.Update(Me.UCCESCALASDataSet)
        Me.BindingSource1.EndEdit()
        MsgBox("Militar Adicionado a Escala")
        GroupBox1.Enabled = False
    Catch EX As Exception
        MsgBox("Erro ao adicionar militar a Escala", MsgBoxStyle.Critical)
    End Try


End Sub

Private Sub AdicionarMilitarAEscalaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AdicionarMilitarAEscalaToolStripMenuItem.Click
    GroupBox1.Enabled = True
    BindingSource1.AllowNew = True
    BindingSource1.AddNew()
End Sub

How do you add your records? Is it from TextBox or DataGridView?

If you are adding records in TextBox:

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ace.oledb.12.0;data source=C:\test.accdb")
Dim cmd As OleDbCommand = New OleDbCommand("Insert Into Table(Field1, Field2, Field3) Values('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "')", con)
con.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()

If you are adding records in DataGridView:
Note: It must have a primary key to work

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ace.oledb.12.0;data source=C:\test.accdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table", con)
        con.Open()
        adpt = New OleDbDataAdapter(cmd)
        'Here one CommandBuilder object is required.
        'It will automatically generate DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object  
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adpt)
        DtSet = New DataSet()
        adpt.Fill(DtSet, "Table")
        DataGridView1.DataSource = DtSet.Tables("Table").DefaultView       
        con.Close()
        con = Nothing
    End Sub

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Validate()
        Me.adpt.Update(Me.DtSet.Tables("Table"))
        Me.DtSet.AcceptChanges()
    End Sub

Happy coding ^_^

Hi jared.geli, thanks for your reply, i add records from a textbox? One question, is there anything worng with my code or missing? Thanks for your help in advance.

João Almeida

To be honest I don't understand your code. It's like you're adding and updating records in datagrid. If you're adding records through text box try my code about adding records in text boxes. You don't need a dataset or datatable in adding records through text boxes. Unless you are using binding source

That's the thing i'm using a binding source.

The one that imports the database through wizard? If so have you tried drag and drop for that? If you use the wizard you can drag the fields in your form and automatically generate codes for it

jared.geli you're the best, thank you

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.