Vb.net Access Database — Example
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.
' Get the UserID from the selected row Dim userID As Integer = Convert.ToInt32(dgvUsers.SelectedRows(0).Cells("UserID").Value)
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" vb.net access database example
' Optional: Display record count lblStatus.Text = $"Loaded dgvUsers.Rows.Count records" End Sub Add this code to the Insert button:
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 Introduction Connecting a VB
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click If dgvUsers.SelectedRows.Count = 0 Then MessageBox.Show("Please select a record to delete") Return End If Dim userID As Integer = Convert.ToInt32(dgvUsers.SelectedRows(0).Cells("UserID").Value)
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
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadData() End Sub