Vb.net Access Database Example -

Using conn As New OleDbConnection(connectionString) Using cmd As New OleDbCommand(query, conn) ' Add parameters to prevent SQL injection cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text) cmd.Parameters.AddWithValue("@LastName", txtLastName.Text) cmd.Parameters.AddWithValue("@Email", txtEmail.Text) cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(txtAge.Text)) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using

Return True End Function Add this to the Update button:

Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click If ValidateInputs() = False Then Exit Sub Dim query As String = "INSERT INTO Users (FirstName, LastName, Email, Age) VALUES (@FirstName, @LastName, @Email, @Age)" vb.net access database example

Private Sub ClearInputs() txtFirstName.Clear() txtLastName.Clear() txtEmail.Clear() txtAge.Clear() txtFirstName.Focus() End Sub Private Sub dgvUsers_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvUsers.CellClick If e.RowIndex >= 0 Then Dim row As DataGridViewRow = dgvUsers.Rows(e.RowIndex) txtFirstName.Text = row.Cells("FirstName").Value.ToString() txtLastName.Text = row.Cells("LastName").Value.ToString() txtEmail.Text = row.Cells("Email").Value.ToString() txtAge.Text = row.Cells("Age").Value.ToString() End If End Sub Here's the complete form code:

[First Name: TextBox1] [Last Name: TextBox2] [Email: TextBox3] [Age: TextBox4] [Load] [Insert] [Update] [Delete] Age) VALUES (@FirstName

Dim query As String = "UPDATE Users SET FirstName=@FirstName, LastName=@LastName, Email=@Email, Age=@Age WHERE UserID=@UserID"

Private Sub LoadData() Dim query As String = "SELECT * FROM Users" Using conn As New OleDbConnection(connectionString) Using adapter As New OleDbDataAdapter(query, conn) Dim table As New DataTable() adapter.Fill(table) dgvUsers.DataSource = table End Using End Using End Sub a contact manager

[DataGridView here] Declare a module-level variable for the connection string:

MessageBox.Show("Record updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) LoadData() End Sub Add this to the Delete button:

Introduction Connecting a VB.NET application to a Microsoft Access database is one of the most common tasks for desktop developers. Whether you're building a small inventory system, a contact manager, or a data entry application, this guide will walk you through everything you need to know.

If Not IsNumeric(txtAge.Text) Then MessageBox.Show("Age must be a number") Return False End If