Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

VB.NET OOP 
with 
MS Access
Step-by-Step Guide to 
Classes and Database Integration

VB.NET OOP Classes


💡 Introduction

This post demonstrates how to use Object-Oriented Programming (OOP) in Visual Basic .NET by creating and using classes that interact with a Microsoft Access database

You'll learn how to structure a simple inventory system using custom classes, properties, and methods, while following a clean architecture approach.

🧱 Step 1: Setup the Access Database

Create an MS Access database file named Inventory.accdb with a single table called Products and the following fields:

  • ProductID (AutoNumber, Primary Key)
  • ProductName (Short Text)
  • Quantity (Number)
  • Price (Currency)

🏗️ Step 2: Create the Product Class

Define a Product class with properties and constructors. This encapsulates product-related data and behavior.

Public Class Product
    Public Property ProductID As Integer
    Public Property ProductName As String
    Public Property Quantity As Integer
    Public Property Price As Decimal

    Public Sub New()
    End Sub

    Public Sub New(id As Integer, name As String, qty As Integer, price As Decimal)
        ProductID = id
        ProductName = name
        Quantity = qty
        Price = price
    End Sub
End Class

🔌 Step 3: Create the Data Access Layer (DAL)

This layer handles all communication with the MS Access database.

Imports System.Data.OleDb

Public Class ProductDAL
    Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventory.accdb"

    Public Function GetAllProducts() As List(Of Product)
        Dim products As New List(Of Product)()
        Using conn As New OleDbConnection(connectionString)
            Dim cmd As New OleDbCommand("SELECT * FROM Products", conn)
            conn.Open()
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            While reader.Read()
                Dim p As New Product(reader("ProductID"), reader("ProductName"), reader("Quantity"), reader("Price"))
                products.Add(p)
            End While
        End Using
        Return products
    End Function
End Class

🖥️ Step 4: Bind Data to WinForms UI

In your Form1.vb, load products using the DAL and bind them to a DataGridView.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dal As New ProductDAL()
    Dim products = dal.GetAllProducts()
    DataGridView1.DataSource = products
End Sub

🧠 Benefits of OOP

  • Encapsulation: Group data and behavior into reusable objects.
  • Maintainability: Easier to update code by modifying class logic.
  • Reusability: Classes can be reused in other projects or components.

🏁 Conclusion

This step-by-step guide shows how to use VB.NET with Object-Oriented Programming principles to manage data in a Microsoft Access database. You learned how to create classes, a DAL layer, and integrate them with WinForms. This approach leads to cleaner, scalable, and more professional VB.NET applications.

🔗 GitHub Sample (Optional)

Visit GitHub Repository

No comments:

Bottom Ad [Post Page]