Vb.net Access Database Example Guide
MessageBox.Show("Record added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) ClearInputs() LoadData() ' Refresh the grid End Sub
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 Function ValidateInputs() As Boolean If String.IsNullOrWhiteSpace(txtFirstName.Text) Then MessageBox.Show("First Name is required") Return False End If vb.net access database example
' Get the UserID from the selected row Dim userID As Integer = Convert.ToInt32(dgvUsers.SelectedRows(0).Cells("UserID").Value)
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: MessageBox
[First Name: TextBox1] [Last Name: TextBox2] [Email: TextBox3] [Age: TextBox4] [Load] [Insert] [Update] [Delete]
MessageBox.Show("Record updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) LoadData() End Sub Add this to the Delete button: MessageBox.Show("Record added successfully!"
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click If dgvUsers.SelectedRows.Count = 0 Then MessageBox.Show("Please select a record to update") Return End If If ValidateInputs() = False Then Exit Sub