Microsoft Access Database Management (VB)


This page discusses how to use VB (Visual Basic) programs to manage Microsoft Access databases.
  1. Check the Microsoft Access Database Help Pages.

  2. Start the Visual Studio Community.
  3. Check the Microsoft Visual Basic Programming and select the following Windows options:
       Start ⇒ All Programs
             ⇒ Visual Studio 2015
  4. Create a New Project.
  5. For example, create a Windows application at C:\VB-workspace\WindowsApplication3\ .

  6. Use the Access to Build a Database and Tables.
  7. Check the Microsoft Access Database Management. Under the application, create a folder such as C:\VB-workspace\WindowsApplication3\Access\ to host the database used by the application. The database created is C:\VB-workspace\WindowsApplication3\Access\Inventory.accdb, which includes only one table Grocery having 4 columns, ID (the primary key), Description, Cost, and Price.




  8. Create the User Interface.
  9. Create a user interface such as


    by using the panes of “All Windows Forms” of “Toolbox” and “Properties.” This user interface includes


    For how to create a Visual Studio user interface, check Creating the Visual Look of Your Program: Introduction to Windows Forms.

  10. Implement the Project.
  11. Write the events’ procedures, for example, by doubly clicking the button “Find:”


    C:\VB-workspace\WindowsApplication3\WindowsApplication3\Form1.vb
     Imports System
     Imports System.Data
     Imports System.Data.OleDb
     Imports System.Data.SqlClient
    
     Public Class Form1
      Dim provider   As String
      Dim dataFile   As String
      Dim connString As String
      Public conn    As OleDbConnection = New OleDbConnection
      Public dr      As OleDbDataReader
    
      Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load
       provider   = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
       dataFile   = "C:\VB-workspace\WindowsApplication3\Access\Inventory.accdb"
       connString = provider & dataFile
       conn.ConnectionString = connString
      End Sub
    
      Private Sub FindButton_Click( sender As Object, e As EventArgs ) Handles FindButton.Click
       conn.Open( )
       DescriptionText.Clear( )
       CostText.Clear( )
       PriceText.Clear( )
    
       Dim str As String
       str = "SELECT * FROM Grocery WHERE (ID = " & IDText.Text & ")"
       Dim cmd As OleDbCommand = New OleDbCommand( str, conn )
       dr = cmd.ExecuteReader
       While dr.Read( )
        DescriptionText.Text = dr("Description").ToString
        CostText.Text  = dr("Cost").ToString
        PriceText.Text = dr("Price").ToString
       End While
       conn.Close( )
      End Sub
     End Class

    This program is to display the detailed information of a grocery item by giving its ID. For how to write a Visual Basic program, check Introduction to the Visual Basic Programming Language.

  12. Build the Project.
  13. Build the project by selecting the following VS options:
       Build ⇒ Build Solution

  14. Execute the Project.
  15. Close the Access and execute the project by selecting the following VS options:
       Debug ⇒ Start Without Debugging
    One example of execution results is as follows: