Adding a new record to Access 2010 db with VB.NET

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
OK, so Ive tried to figure out how to fix this (Im pretty new to programming) but Im stuck, so I decided to ask for help. Basically, what I want to do is to use a form in VB 2010 to add a new record to my database in Access 2010. Im thinking the da.Update(dt) is the problem, however Im not sure so any help would be appreciated.Imports System.Data.OleDb
Public Class frmNewStudent
Dim con As New OleDbConnection

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

Dim dt As New DataTable
Dim ds As New DataSet

con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|sdb1.accdb"

con.Open()

ds.Tables.Add(dt)

Dim da As New OleDbDataAdapter

da = New OleDbDataAdapter("SELECT * FROM StudentDetails", con)

da.Fill(dt)


Dim newRow As DataRow = dt.NewRow

If txtAdd1.Text = "" Or txtCity.Text = "" Or txtCourse.Text = "" Or txtName.Text = "" Or txtPCode.Text = "" Or txtTown.Text = "" Or cboForm.Text = "" Or cboGender.Text = "" Then
MsgBox("A field(s) has been left blank. Please rectify this.")
End If

newRow.Item(1) = txtName.Text
newRow.Item(2) = txtTown.Text
newRow.Item(3) = txtCity.Text
newRow.Item(4) = txtAdd1.Text
newRow.Item(5) = cboForm.SelectedText
newRow.Item(6) = cboGender.Text
newRow.Item(7) = txtCourse.SelectedText
newRow.Item(8) = txtPoints.Text
newRow.Item(9) = txtUsername.Text
newRow.Item(10) = txtPassword.Text

MsgBox("We are here (1)...")
dt.Rows.Add(newRow)

MsgBox("We are here (2)...")
Dim cb As New OleDbCommandBuilder(da)
MsgBox(dt.Rows.Count)
da.Update(dt)
MsgBox("We are here (3)...")
MsgBox("Record successfully saved", vbInformation)
con.Close()

View the full article
 
Back
Top