Lesson 24: Advanced Database Techniques in VB6

Learn to create custom navigation buttons, manipulate records, and build professional database interfaces

Key Takeaway

Visual Basic provides powerful database manipulation methods that allow you to create custom navigation interfaces and add, update, and delete records programmatically.

Welcome to Lesson 24 of our Visual Basic 6 Tutorial! In this lesson, you'll learn advanced database techniques building on the foundation from Lesson 23. We'll create custom navigation buttons, implement record manipulation functionality, and design a professional database interface without relying on the visible Data Control.

24.1 Custom Navigation Buttons

In Lesson 23, you used the Data Control's built-in navigation buttons. Now, you'll create your own custom navigation buttons for a more professional interface. The key methods for record navigation are:

MoveFirst

Moves to the first record in the recordset

data_navigator.Recordset.MoveFirst

MovePrevious

Moves to the previous record in the recordset

data_navigator.Recordset.MovePrevious

MoveNext

Moves to the next record in the recordset

data_navigator.Recordset.MoveNext

MoveLast

Moves to the last record in the recordset

data_navigator.Recordset.MoveLast

24.1.1 Implementation Steps

To implement custom navigation buttons:

1Hide the Data Control

Set the Data Control's Visible property to False

2Add Command Buttons

Create four buttons: First, Previous, Next, Last

3Write Event Handlers

Implement the navigation methods in button click events

NavigationButtons.frm
Private Sub cmdFirst_Click()
    ' Move to the first record
    data_navigator.Recordset.MoveFirst
End Sub

Private Sub cmdPrevious_Click()
    ' Move to the previous record
    data_navigator.Recordset.MovePrevious
End Sub

Private Sub cmdNext_Click()
    ' Move to the next record
    data_navigator.Recordset.MoveNext
End Sub

Private Sub cmdLast_Click()
    ' Move to the last record
    data_navigator.Recordset.MoveLast
End Sub
                        
Custom Navigation Buttons Interface
Figure 24.1: Database application with custom navigation buttons

24.2 Record Manipulation

Beyond navigation, you can manipulate records with these essential methods:

AddNew

Creates a new blank record ready for data entry

data_navigator.Recordset.AddNew

Update

Saves changes made to the current record

data_navigator.Recordset.Update

Delete

Deletes the current record from the database

data_navigator.Recordset.Delete

24.2.1 Implementation Steps

To implement record manipulation:

1Add Manipulation Buttons

Create buttons: Add New, Save, Delete

2Write Event Handlers

Implement record manipulation methods

3Add Confirmation

Implement confirmation dialogs for critical operations

RecordManipulation.frm
Private Sub cmdAdd_Click()
    ' Create a new record
    data_navigator.Recordset.AddNew
End Sub

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

Private Sub cmdDelete_Click()
    ' Confirm deletion
    Dim response As Integer
    response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion)
    
    If response = vbYes Then
        data_navigator.Recordset.Delete
        data_navigator.Recordset.MoveNext
        If data_navigator.Recordset.EOF Then
            data_navigator.Recordset.MoveLast
        End If
    End If
End Sub
                        

Advanced Database Browser

Try browsing and manipulating customer records using the custom buttons:

Customer Name: Maria Anders
Company: Alfreds Futterkiste
City: Berlin
Record 1 of 5

24.3 Professional Interface Design

With custom navigation and record manipulation buttons, you can design a more professional and user-friendly interface:

Custom Layout

Design your own button layout for optimal usability

Visual Design

Apply consistent styling and color schemes

Validation

Add data validation before saving records

Lesson Summary

In this lesson, you've learned advanced database techniques in VB6:

Custom Navigation

Creating your own navigation buttons with MoveFirst, MovePrevious, MoveNext, MoveLast

Record Manipulation

Adding, saving, and deleting records with AddNew, Update, and Delete

Professional UI

Designing custom interfaces without the visible Data Control

Confirmation Dialogs

Implementing user confirmation for critical operations

Important Note

When implementing custom navigation, always check for BOF (Beginning of File) and EOF (End of File) conditions to prevent errors when moving beyond record boundaries.

Practice Exercises

Enhance your database application with these exercises:

Exercise 1: Boundary Checking

Implement BOF and EOF checks in your navigation buttons to prevent errors at record boundaries.

Exercise 2: Data Validation

Add validation to ensure required fields are filled before saving a record.

Exercise 3: Undo Functionality

Implement an "Undo" button to cancel changes to the current record.

Exercise 4: Record Counter

Display the current record position and total records (e.g., "Record 3 of 25").

Exercise 5: Search Function

Add a search feature to find records by customer name or ID.

Next Lesson

Continue your VB6 journey with Lesson 25: Using ADO Control.

Related Resources

VB6 Database Basics

Review database fundamentals from Lesson 23

View Lesson

ADO Control Tutorial

Learn about the ADO control in the next lesson

Next Lesson

VB6 Database Samples

Practical VB6 database code samples

View Examples

DataGrid Control

Learn to display multiple records with DataGrid

Lesson 26