Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB.net to ms Access database add new record using oledb.commandbuilder how?
VB
dr.item(0)=textbox1.text
dr.item(1)=textbox2.text


What I have tried:

VB
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow

dsNewRow = ds.Tables("AddressBook").NewRow()

dsNewRow.Item("FirstName") = txtFirstName.Text
dsNewRow.Item("Surname") = txtSurname.Text

ds.Tables("AddressBook").Rows.Add(dsNewRow)

da.Update(ds, "AddressBook")

MessageBox.Show("New Record added to the Database")
Posted
Updated 6-Aug-20 22:30pm
v2
Comments
Richard Deeming 7-Aug-20 6:16am
   
REPOST
This is the same question your posted yesterday:
How to save data visual basic to ms access database in index no such as dr(0), dr(1)[^]

If you want to improve your question, click the green "Improve question" link and edit your question. Do not post your update as a "new" question.

Seems you started from here but have not followed it completely: Visual Basic .NET: Add a New Record[^] - you should read line by line and understand.

A sample you can try:
VB
Private Function CreateConnection() As OleDb.OleDbConnection
    Return New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DBCaseStudy.mdb;Persist Security Info=False")
End Function

Private Sub btnRegCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegCreate.Click
    Using conn As OleDb.OleDbConnection = CreateConnection()
        Using comd As New OleDb.OleDbCommand("INSERT INTO tblAdmins ([ID], [Firstname], [Lastname], [Username], [Password] VALUES (@ID, @Firstname, @Lastname, @Username, @Password)", conn)
            comd.Parameters.AddWithValue("@ID", txtRegID.Text)
            comd.Parameters.AddWithValue("@Firstname", txtRegFirst.Text)
            comd.Parameters.AddWithValue("@Lastname", txtRegLast.Text)
            comd.Parameters.AddWithValue("@Username", txtRegUsername.Text)
            comd.Parameters.AddWithValue("@Password", txtRegPass.Text)
            
            conn.Open()
            Try
                comd.ExecuteNonQuery()
                MsgBox("Record Appended", MsgBoxStyle.Information, "Successfully Added!")
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End Using
    End Using
End Sub

UPDATE:
For commandbuilder specific: Dataadapter with CommandBuilder - OLEDB[^]
VB
Imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As OleDbConnection
        Dim oledbAdapter As OleDbDataAdapter
        Dim oledbCmdBuilder As OleDbCommandBuilder
        Dim ds As New DataSet
        Dim i As Integer
        Dim sql As String
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        connection = New OleDbConnection(connetionString)
        sql = "select * from tblUsers"
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(sql, connection)
            oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
            oledbAdapter.Fill(ds)
            For i = 0 To ds.Tables(0).Rows.Count - 1
                ds.Tables(0).Rows(i).Item(2) = "neweamil@email.com"
            Next
            oledbAdapter.Update(ds.Tables(0))
            connection.Close()
            MsgBox("Email address updates !")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
   
v2
Comments
Member 13554586 7-Aug-20 4:29am
   
Sir using OLEDB.COMMANDBUILDER INSERT NEW RECORDS to ms Access database in VB.net
Sandeep Mewara 7-Aug-20 5:08am
   
Here:
http://vb.net-informations.com/dataadapter/dataadapter-commandbuilder-oledb.htm
Sir using OLEDB.COMMANDBUILDER INSERT NEW RECORDS to ms Access database in VB.net
   
Comments
Richard Deeming 7-Aug-20 6:15am
   
In what way is this meant to be a "solution" to your question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900