jbutler483
jbutler483

Reputation: 24559

Add a New Record to Microsoft Access Database using VB.Net?

I have a form called (Registration), and a database called (UsersLogin) with a table called (tbl_user) with the fields

I want to not only be able to edit records (I can already do this), but I cant seem to be able to add/create a new row/record. Below is the class registration I am using so far to update a database row, but I can't seem to be able to 'add' a new record (rather than simply updating):

Imports System.Data
Imports System.Data.SqlClient
Public Class Registration
    Private Sub Registration_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.Tbl_userTableAdapter.Fill(Me.UsersLoginDataSet.tbl_user)
        Catch ex As Exception
            MessageBox.Show("The database file is unavailable", "Database Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.Close()
        End Try
    End Sub

    Private Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
        Try
            Me.Validate()
            Me.TbluserBindingSource.EndEdit()
            Me.Tbl_userTableAdapter.Update(Me.UsersLoginDataSet)
            MessageBox.Show("Updates to the database have been successful.", "Successful Updates", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Updates to the database have failed.", "Unsuccessful Updates", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        LoginScreen.Show()
        Me.Hide()
    End Sub
End Class

The dataset is called UsersLoginDataSet.xsd

Anyone know how to add a new user (i.e. make a new row), and hence write this to a table?

I have looked for several solutions to this on the web, but have not been able to add a new row and hence "fill" the record in the database.

Could anyone possibly explain how this should be done?

Upvotes: 1

Views: 13915

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25371

The details you provided are not enough, but I will try to answer according to your comment. Below is a example from MSDN about how to add a new row. When you create the new row, you have to use NewRow() on the table, and after creating it use Rows.Add() on your table to add the new row:

Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()

newCustomersRow("CustomerID") = "ALFKI"
newCustomersRow("CompanyName") = "Alfreds Futterkiste"

DataSet1.Tables("Customers").Rows.Add(newCustomersRow)

Form more details, you read the MSDN article.

Upvotes: 3

Related Questions