Connect Visual Basic.Net with MS Access Database (.accdb) with Password is a secure way to manage Database. This tutorial is going to explain how to connect Visual Basic.Net with MS Access Database using code. Visual Basic .net requires an MS Access driver for connection. The driver is downloaded according to the installed version of MS Office. MS Office version is based on 32 or 64bit. Therefore, if you have already installed MS Office, you should know the office is 32 or 64bit.
Visual Basic.Net with MS Access Database (.accdb) with Password
From the following the link, you can download and install Microsoft Access Database Engine 2010 Redistributable
Now start MS Visual Basic .net, create a new project and save it. Open MS Access and create a new database with password protected. In the video, it is explained how to set a password on the MS Access database.
It is good practice, if you use a different Module for database connection, you can change the Module name and form name. Here I am using Module and form1, for better understanding.
Copy and paste the code into Module1
Imports System.Data.OleDb Module Module1 Public DBConnection As New OleDbConnection Sub connectDatabase() Try If DBConnection.State = ConnectionState.Closed Then DBConnection.ConnectionString = "Provider=microsoft.ace.oledb.12.0; data source=" & Application.StartupPath & "\Database.accdb; Jet OLEDB:Database Password=12345 ;" DBConnection.Open() Form1.Text = " Connected" Else Form1.Text = " Connection is already Opend" End If Catch ex As Exception Form1.Text = ex.Message.ToString End Try End Sub End Module
The next step is to connect the database with form1, so we will use the Sub connectDatabase() at form1 load.
Copy and paste the code at form start-up
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Module1.connectDatabase() End Sub
Now run the program, you will see at form1.text “Connected” means now database with the password is connected. If you set the password wrong, it will show “Not a valid password” at form1.text