Delete Access Table - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

In this tutorial, we will delete an entire table in an Access Database from a visual basic form. We will delete the following table: Items:

delete access table in visual baisc .net

Note: this code will delete the entire table and its records. Once it is deleted, you cannot retrieve the records back. Make sure you have a backup table before you proceed.

Add the following code before the (Public Class Form1) line:

Imports System.Data.OleDb

Then add the following declarations below the (Public Class Form1) line

Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
        

The following is the Delete button click event code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim okToDelete As MsgBoxResult = MsgBox("Are you sure you want to delete the desired table", MsgBoxStyle.YesNo)
        If okToDelete = MsgBoxResult.Yes Then
            provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
            dataFile = "C:\Users\Jimmy\Documents\Test Database.accdb"
            connString = provider & dataFile
            myConnection.ConnectionString = connString

            myConnection.Open()
            Dim str As String = "DROP TABLE Items"
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                myConnection.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        ElseIf okToDelete = MsgBoxResult.No Then
        End If
    End Sub


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.