VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Database Application with VB6 & VB.NET

Learn to create database applications using Visual Basic Data Control with practical examples in both VB6 and VB.NET

Database Applications in Visual Basic

Visual Basic provides powerful tools for creating database applications. Using the Data Control component, you can connect to databases, navigate records, and perform CRUD (Create, Read, Update, Delete) operations with minimal code.

Key Concepts

  • Data Control: The visual component that connects your form to a database
  • Recordset: The set of records from a database table or query
  • Bound Controls: UI elements (textboxes, labels) connected to database fields
  • Navigation: Moving between records using MoveFirst, MoveNext, etc.

VB6 Database Application

The Interface

VB6 Database Application Interface

A simple database navigation interface with bound controls and navigation buttons.

How It Works

The VB6 Data Control handles the database connection and record navigation. You can use its methods to programmatically navigate records:

Interactive Demo

Title:
Author:
ISBN:
Year:

VB6 Code Implementation

Visual Basic 6.0
Legacy Windows Application
Private Sub cmdFirst_Click()
    ' Move to the first record
    dtaBooks.Recordset.MoveFirst
End Sub

Private Sub cmdNext_Click()
    ' Move to the next record
    dtaBooks.Recordset.MoveNext
    ' Check if we've reached the end
    If dtaBooks.Recordset.EOF Then
        dtaBooks.Recordset.MoveLast
        MsgBox "End of records!", vbInformation
    End If
End Sub

Private Sub cmdPrevious_Click()
    ' Move to the previous record
    dtaBooks.Recordset.MovePrevious
    ' Check if we're before the first record
    If dtaBooks.Recordset.BOF Then
        dtaBooks.Recordset.MoveFirst
        MsgBox "Beginning of records!", vbInformation
    End If
End Sub

Private Sub cmdLast_Click()
    ' Move to the last record
    dtaBooks.Recordset.MoveLast
End Sub

Private Sub cmdAdd_Click()
    ' Add a new record
    dtaBooks.Recordset.AddNew
    ' Clear fields for new data entry
    txtTitle.Text = ""
    txtAuthor.Text = ""
    txtISBN.Text = ""
    txtYear.Text = ""
    txtTitle.SetFocus
End Sub

Private Sub cmdSave_Click()
    ' Save the current record
    dtaBooks.Recordset.Update
    MsgBox "Record saved successfully!", vbInformation
End Sub

Private Sub cmdDelete_Click()
    ' Delete the current record with confirmation
    If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion) = vbYes Then
        dtaBooks.Recordset.Delete
        dtaBooks.Recordset.MoveNext
        If dtaBooks.Recordset.EOF Then
            dtaBooks.Recordset.MoveLast
        End If
    End If
End Sub

VB.NET Equivalent

Transition to VB.NET

In VB.NET, the Data Control was replaced with more powerful ADO.NET components. While the concepts are similar, the implementation differs:

  • Data Control → BindingSource: Handles data navigation
  • Recordset → DataTable/DataSet: Represents the data
  • ADO → ADO.NET: Modern data access technology
  • Bound Controls → Data Binding: More flexible binding system

Key Differences

  • VB.NET uses disconnected data architecture
  • Data binding is more flexible and powerful
  • ADO.NET supports multiple database providers
  • Better support for complex data operations

VB.NET Code Implementation

Visual Basic .NET
Modern .NET Application
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Load data from database
    Dim connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=books.mdb"
    Dim sql = "SELECT * FROM Books"
    
    Using conn As New OleDbConnection(connectionString)
        Dim adapter As New OleDbDataAdapter(sql, conn)
        Dim ds As New DataSet()
        adapter.Fill(ds, "Books")
        
        ' Set up binding source
        BindingSource1.DataSource = ds.Tables("Books")
        
        ' Bind controls
        txtTitle.DataBindings.Add("Text", BindingSource1, "Title")
        txtAuthor.DataBindings.Add("Text", BindingSource1, "Author")
        txtISBN.DataBindings.Add("Text", BindingSource1, "ISBN")
        txtYear.DataBindings.Add("Text", BindingSource1, "Year")
    End Using
End Sub

Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
    ' Move to the first record
    BindingSource1.MoveFirst()
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    ' Move to the next record
    BindingSource1.MoveNext()
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    ' Add a new record
    BindingSource1.AddNew()
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    ' Save changes to the database
    Try
        BindingSource1.EndEdit()
        ' Update database here (code omitted for brevity)
        MessageBox.Show("Record saved successfully!")
    Catch ex As Exception
        MessageBox.Show("Error saving record: " & ex.Message)
    End Try
End Sub

VB6 vs VB.NET Database Features

Data Access

VB6: Uses ADO with connected architecture

VB.NET: Uses ADO.NET with disconnected architecture

Data Binding

VB6: Simple property binding to Data Control

VB.NET: Flexible and complex binding capabilities

Database Support

VB6: Primarily Access, SQL Server via ODBC

VB.NET: Wide range via providers (SQL, Oracle, MySQL, etc.)

Security

VB6: Limited security features

VB.NET: Integrated with .NET security model

Database Development Best Practices

For VB6 Applications

  • Always include error handling for database operations
  • Use parameterized queries to prevent SQL injection
  • Close database connections when not in use
  • Validate data before saving to the database

For VB.NET Applications

  • Utilize the Using statement for resource management
  • Take advantage of ADO.NET's disconnected model
  • Use Entity Framework for object-relational mapping
  • Implement proper data validation and error handling

Learning Resources

VB6 Database Programming

Master database development with VB6 using practical examples and real-world projects

View Book

VB.NET and Databases

Modern database techniques with VB.NET, Entity Framework, and LINQ

View Book

Video Tutorials

Step-by-step video guides for building database applications

Watch Now

Sample Projects

Download complete database application projects for both VB6 and VB.NET

Download