Lesson 29: Creating an Electronic Library in VB6

Build a secure database application with user authentication, registration, and advanced book management features

Key Takeaway

The electronic library application demonstrates how to create a secure VB6 database system with user authentication, registration, and advanced search capabilities using ADO controls and SQL queries.

Welcome to Lesson 29 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to create a complete electronic library application with user authentication and database management. This application will include a welcome menu, registration form, login system, and a main library interface for managing books.

Application Overview

The electronic library application will follow this workflow:

1Welcome Menu

Users choose to register or login

2Registration

New users create an account with username and password

3Login

Existing users authenticate to access the library

4Main Library

Authenticated users can browse, search, and manage books

Application Workflow
Figure 29.1: Application Workflow

29.1 The Welcome Menu

The welcome menu serves as the entry point to the application, allowing users to register or login.

Welcome Menu
Figure 29.2: The Welcome Menu

Control Properties

Table 29.1: Welcome Menu Control Properties
Control Property Value
Form Name main_menu
Button 1 Name
Caption
cmdRegister
Register
Button 2 Name
Caption
cmdLogin
Login
Button 3 Name
Caption
cmdCancel
Cancel

Code Implementation

WelcomeMenu.frm
Private Sub cmdLogin_Click()
    main_menu.Hide
    Login_form.Show
End Sub

Private Sub cmdRegister_Click()
    main_menu.Hide
    Register.Show
End Sub
                        

29.2 The Registration Form

New users can create an account through the registration form, which securely stores their credentials.

Registration Form
Figure 29.3: The Registration Form

Control Properties

Table 29.2: Registration Form Control Properties
Control Property Value
Form Name Register
Textbox 1 Name txtName
Textbox 2 Name
PasswordChar
txtpassword
*
Button 1 Name
Caption
cmdConfirm
Confirm
Button 2 Name
Caption
cmdClear
Clear
Button 3 Name
Caption
cmdCancel
Cancel
ADO Control Name UserInfo

Code Implementation

Registration.frm
Private Sub cmdCancel_Click()
    End
End Sub

Private Sub cmdClear_Click()
    txtName.Text = ""
    txtpassword.Text = ""
End Sub

Private Sub cmdConfirm_Click()
    UserInfo.Recordset.Fields("username") = txtName.Text
    UserInfo.Recordset.Fields("password") = txtpassword.Text
    UserInfo.Recordset.Update
    Register.Hide
    Login_form.Show
End Sub

Private Sub Form_Load()
    UserInfo.Recordset.AddNew
End Sub
                        

29.3 The Login Menu

Existing users authenticate using their credentials to access the electronic library.

Login Menu
Figure 29.4: The Login Menu

Control Properties

Table 29.3: Login Menu Control Properties
Control Property Value
Textbox 1 Name txtName
Textbox 2 Name txtpassword
Button Name
Caption
cmdLogin
Login
Form Name Login_form

Code Implementation

Login.frm
Private Sub cmdLogin_Click()
    Dim usrname As String
    Dim psword As String
    Dim usernam As String
    Dim pssword As String
    Dim Msg As String

    Register.UserInfo.Refresh
    usrname = txtName.Text
    psword = txtpassword.Text
    
    Do Until Register.UserInfo.Recordset.EOF
        If Register.UserInfo.Recordset.Fields("username").Value = usrname And _ 
            Register.UserInfo.Recordset.Fields("password").Value = psword Then
            Login_form.Hide
            frmLibrary.Show
            Exit Sub
        Else
            Register.UserInfo.Recordset.MoveNext
        End If
    Loop
    
    Msg = MsgBox("Invalid password, try again!", vbOKCancel)
    If (Msg = 1) Then
        Login_form.Show
        txtName.Text = ""
        txtpassword = ""
    Else
        End
    End If
End Sub
                        

29.4 The Electronic Library

The main library interface allows authenticated users to browse, search, add, and manage books in the database.

Library Design Interface
Figure 29.5: Design Interface
Library Runtime Interface
Figure 29.6: Runtime Interface

Control Properties

Table 29.4: Library Control Properties
Control Property Value
Form Name frmLibrary
ADO Control Name
Visible
adoLibrary
False
DataGrid Name DataLibrary
Textbox 1 Name txtTitleA
Textbox 2 Name txtAuthor
Textbox 3 Name txtPublisher
Textbox 4 Name txtYear
Textbox 5 Name txtCategory

Search Functionality with SQL

SearchButton.frm
Private Sub cmdSearch_Click()
    DataLibrary.Visible = True
    Dim SearchString As String
    SearchString = TxtSearch.Text

    If Opt_ISBN.Value = True Then
        AdoLibrary.RecordSource = "SELECT * FROM book WHERE ISBN='" & SearchString & "'"
    ElseIf Opt_Author.Value = True Then
        'Search for Author that starts with the Search String
        AdoLibrary.RecordSource = "SELECT * FROM book WHERE Author Like '" & SearchString & "%'"
    ElseIf Opt_Title.Value = True Then
        AdoLibrary.RecordSource = "SELECT * FROM book WHERE Title Like '" & SearchString & "%'"
    End If
    
    AdoLibrary.Refresh
    
    'To reset the column width of datagrid DataLibrary
    With DataLibrary
        .Columns(0).Width = 2200
        .Columns(1).Width = 4500
        .Columns(2).Width = 2800
        .Columns(3).Width = 2000
        .Columns(4).Width = 800
        .Columns(5).Width = 1500
    End With

    DataLibrary.Visible = True
End Sub
                        

Note: The % symbol is a wildcard character in SQL. Using SearchString with % means it will search for any combination starting with the SearchString. For example, SearchString="New" will include Newyork, Newport, NewCastle, Newton, etc.

Additional Library Functions

LibraryFunctions.frm
Private Sub cmdDelete_Click()
    Confirm = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
    If Confirm = vbYes Then
        adoLibrary.Recordset.Delete
        MsgBox "Record Deleted!", , "Message"
    Else
        MsgBox "Record Not Deleted!", , "Message"
    End If
End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Sub cmdNew_Click()
    adoLibrary.Recordset.AddNew
End Sub

Private Sub cmdFirst_Click()
    AdoLibrary.Recordset.MoveFirst
End Sub

Private Sub cmdNext_Click()
    If Not adoLibrary.Recordset.EOF Then
        adoLibrary.Recordset.MoveNext
        If adoLibrary.Recordset.EOF Then
            adoLibrary.Recordset.MovePrevious
        End If
    End If
End Sub

Private Sub cmdPrevious_Click()
    If Not adoLibrary.Recordset.BOF Then
        adoLibrary.Recordset.MovePrevious
        If adoLibrary.Recordset.BOF Then
            adoLibrary.Recordset.MoveNext
        End If
    End If
End Sub

Private Sub cmdLast_Click()
    AdoLibrary.Recordset.MoveLast
End Sub

Private Sub cmdSave_Click()
    adoLibrary.Recordset.Fields("Title").Value = txtTitle.Text
    adoLibrary.Recordset.Fields("Author").Value = txtAuthor.Text
    adoLibrary.Recordset.Update
End Sub
                        

Lesson Summary

In this lesson, you've built a complete electronic library application with:

User Authentication

Secure login system with username and password verification

Registration System

New user account creation with password protection

Database Management

CRUD operations for book records using ADO controls

Advanced Search

SQL-based search functionality with wildcard pattern matching

Important Note

This application demonstrates how to create a secure, user-authenticated database application in VB6, which can be adapted for various business applications requiring user accounts and data management.

Next Lesson

Continue your VB6 journey with Lesson 30: Animation Techniques Part 1.

Related Resources

Database Basics

Review database fundamentals from Lesson 23

View Lesson

ADO Control

Database connection fundamentals

View Lesson

SQL Queries

Advanced query techniques

View Lesson

VB6 Samples

Explore more VB6 code examples

View Resource