Vb.net Access Database Example Now
' Get the UserID from the selected row Dim userID As Integer = Convert.ToInt32(dgvUsers.SelectedRows(0).Cells("UserID").Value)
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)"
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click LoadData() End Sub Private Sub LoadData() Dim query As String = "SELECT * FROM Users"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadData() End Sub vb.net access database example
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
Return True End Function Add this to the Update button:
If Not IsNumeric(txtAge.Text) Then MessageBox.Show("Age must be a number") Return False End If ' Get the UserID from the selected row
' Optional: Display record count lblStatus.Text = $"Loaded dgvUsers.Rows.Count records" End Sub Add this code to the Insert button:
[DataGridView here] Declare a module-level variable for the connection string:
Dim query As String = "UPDATE Users SET FirstName=@FirstName, LastName=@LastName, Email=@Email, Age=@Age WHERE UserID=@UserID" Age) VALUES (@FirstName
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.
Dim result As DialogResult = MessageBox.Show("Are you sure you want to delete this record?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If result = DialogResult.Yes Then Dim query As String = "DELETE FROM Users WHERE UserID=@UserID" Using conn As New OleDbConnection(connectionString) Using cmd As New OleDbCommand(query, conn) cmd.Parameters.AddWithValue("@UserID", userID) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using MessageBox.Show("Record deleted successfully!") LoadData() ClearInputs() End If End Sub Add these utility methods: