Skip to content

Quick Start

This guide will help you get started with the cDataBase class library and create basic database applications.


Prerequisites

Required Files

Ensure the following files have been added to your project:

FileLocationDescription
VBMAN.dllProject ReferenceCompiled COM component
Microsoft ActiveX Data Objects 2.8 LibraryProject ReferenceADO core library

Adding to Project

  1. Open VB6 project
  2. Menu: ProjectReferences
  3. Check the following references:
    • VBMAN (VBMAN.dll)
    • Microsoft ActiveX Data Objects 2.8 Library

Client Quick Start

Step 1: Create Form

Create a new form and add the following controls:

  • 1 TextBox (txtLog) - Display logs (MultiLine = True)
  • 1 CommandButton (cmdConnect) - Connect to database
  • 1 CommandButton (cmdQuery) - Execute query

Step 2: Write Code

vb
Option Explicit

' Declare database object (use class from VBMAN.dll)
Private WithEvents m_DB As VBMAN.cDataBase

Private Sub Form_Load()
    ' Create database object
    Set m_DB = New VBMAN.cDataBase

    ' Connect to SQL Server database
    If m_DB.Connect(VBMAN.enumDbType_MsSql, _
                    "127.0.0.1,1433", _
                    "sa", _
                    "Sa123456", _
                    "master") Then
        LogMessage "Database connection successful"
    Else
        LogMessage "Database connection failed: " & m_DB.LastErr
    End If
End Sub

Private Sub cmdQuery_Click()
    On Error GoTo EH

    ' Execute query
    If m_DB.Sql("SELECT TOP 10 * FROM sys.tables").Query Then
        ' Get result set
        Dim i As Long
        For i = 1 To m_DB.Rows.Count
            LogMessage "Table name: " & m_DB.Rows(i)("name")
        Next
    Else
        LogMessage "Query failed: " & m_DB.LastErr
    End If

    Exit Sub

EH:
    LogMessage "Error: " & Err.Description
End Sub

' ====== Helper Functions ======

Private Sub LogMessage(sMessage As String)
    txtLog.Text = txtLog.Text & Format$(Now, "hh:mm:ss") & " - " & sMessage & vbCrLf
    txtLog.SelStart = Len(txtLog.Text)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Disconnect
    If Not m_DB Is Nothing Then
        m_DB.Disconnect
    End If
End Sub

Step 3: Run Test

  1. Press F5 to run the program
  2. Click "Connect to Database"
  3. Click "Execute Query"
  4. View log output

Server Quick Start

Step 1: Create Form

Create a new form and add the following controls:

  • 1 TextBox (txtPort) - Port number
  • 1 CommandButton (cmdStart) - Start server
  • 1 ListBox (lstResults) - Display results
  • 1 TextBox (txtLog) - Display logs

Step 2: Write Code

vb
Option Explicit

Private WithEvents m_DB As VBMAN.cDataBase

Private Sub Form_Load()
    Set m_DB = New VBMAN.cDataBase
    txtPort.Text = "1433"
End Sub

Private Sub cmdStart_Click()
    On Error GoTo EH

    ' Connect to database
    If m_DB.Connect(VBMAN.enumDbType_MsSql, _
                    "127.0.0.1," & txtPort.Text, _
                    "sa", _
                    "Sa123456", _
                    "master") Then
        LogMessage "Database service started"
        LoadData
    Else
        LogMessage "Start failed: " & m_DB.LastErr
    End If

    Exit Sub

EH:
    LogMessage "Error: " & Err.Description
End Sub

Private Sub LoadData()
    ' Query data
    If m_DB.Sql("SELECT name FROM sys.tables ORDER BY name").Fetch Then
        Dim i As Long
        lstResults.Clear
        For i = 1 To m_DB.Rows.Count
            lstResults.AddItem m_DB.Rows(i)("name")
        Next
        LogMessage "Loaded " & m_DB.Rows.Count & " records"
    End If
End Sub

' ====== Helper Functions ======

Private Sub LogMessage(sMessage As String)
    txtLog.Text = txtLog.Text & Format$(Now, "hh:mm:ss") & " - " & sMessage & vbCrLf
    txtLog.SelStart = Len(txtLog.Text)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Not m_DB Is Nothing Then
        m_DB.Disconnect
    End If
End Sub

Complete Example: User Management

Server Code

vb
Option Explicit

Private WithEvents m_DB As VBMAN.cDataBase

Private Sub Form_Load()
    Set m_DB = New VBMAN.cDataBase

    ' Connect to database
    m_DB.Connect VBMAN.enumDbType_MsSql, "127.0.0.1,1433", "sa", "pwd", "mydb"

    ' Create user table (if not exists)
    CreateUserTable

    ' Load user list
    LoadUsers
End Sub

' Create user table
Private Sub CreateUserTable()
    Dim sSql As String
    sSql = "IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'users') " & _
           "CREATE TABLE users (id INT IDENTITY(1,1) PRIMARY KEY, " & _
           "name NVARCHAR(50), age INT, email NVARCHAR(100))"

    m_DB.Sql(sSql).Exec
End Sub

' Load user list
Private Sub LoadUsers()
    If m_DB.Sql("SELECT * FROM users ORDER BY id").Fetch Then
        Dim i As Long
        lstUsers.Clear
        For i = 1 To m_DB.Rows.Count
            lstUsers.AddItem m_DB.Rows(i)("name") & " - " & m_DB.Rows(i)("age")
        Next
    End If
End Sub

' Add user
Private Sub cmdAddUser_Click()
    ' Use parameterized query to prevent SQL injection
    If m_DB.Sql("INSERT INTO users (name, age, email) VALUES (?, ?, ?)") _
        .Param("name", txtName.Text, VBMAN.adVarWChar) _
        .Param("age", CLng(txtAge.Text), VBMAN.adInteger) _
        .Param("email", txtEmail.Text, VBMAN.adVarWChar) _
        .ExecParam Then

        LogMessage "User added successfully, ID: " & m_DB.LastInsertId
        LoadUsers
    Else
        LogMessage "Add failed: " & m_DB.LastErr
    End If
End Sub

' Delete user
Private Sub cmdDeleteUser_Click()
    If m_DB.Sql("DELETE FROM users WHERE id = ?") _
        .Param("id", CLng(txtId.Text), VBMAN.adInteger) _
        .ExecParam Then

        LogMessage "User deleted successfully"
        LoadUsers
    End If
End Sub

Private Sub LogMessage(sMessage As String)
    txtLog.Text = txtLog.Text & Format$(Now, "hh:mm:ss") & " - " & sMessage & vbCrLf
    txtLog.SelStart = Len(txtLog.Text)
End Sub

Client Code

vb
Option Explicit

Private WithEvents m_DB As VBMAN.cDataBase

Private Sub Form_Load()
    Set m_DB = New VBMAN.cDataBase
    m_DB.Connect VBMAN.enumDbType_MsSql, "127.0.0.1,1433", "sa", "pwd", "mydb"

    LoadUsers
End Sub

Private Sub LoadUsers()
    ' Use paginated query
    If m_DB.Sql("SELECT * FROM users").Page(1, 10).Fetch Then
        Dim i As Long
        lstUsers.Clear
        For i = 1 To m_DB.Rows.Count
            lstUsers.AddItem m_DB.Rows(i)("name") & " - " & m_DB.Rows(i)("age")
        Next
    End If
End Sub

Private Sub cmdSearch_Click()
    ' Use parameterized query for search
    If m_DB.Sql("SELECT * FROM users WHERE name LIKE ?") _
        .Param("name", "%" & txtSearch.Text & "%", VBMAN.adVarWChar) _
        .QueryParam Then

        Dim i As Long
        lstResults.Clear
        For i = 1 To m_DB.Rows.Count
            lstResults.AddItem m_DB.Rows(i)("name")
        Next
    End If
End Sub

Common Questions

Q1: Compilation Error "User-defined type not defined"

Cause: VBMAN.dll or Microsoft ActiveX Data Objects 2.8 Library not referenced

Solution:

  1. Menu: ProjectReferences
  2. Check VBMAN and Microsoft ActiveX Data Objects 2.8 Library

Q2: Connection Failed "Cannot connect to database"

Cause: Connection string error or database service not started

Solution:

  • Check if database service is running
  • Verify connection parameters (address, port, username, password)
  • Check firewall settings

Q3: Query Returns Empty Results

Cause: SQL statement error or table does not exist

Solution:

  • Use m_DB.LastErr to view error information
  • Check SQL statement syntax
  • Verify table name and field name

Q4: How to Execute Transaction Operations

vb
' Start transaction
m_DB.TransBegin

' Execute multiple operations
m_DB.Sql("INSERT INTO table1 ...").Exec
m_DB.Sql("INSERT INTO table2 ...").Exec

' Commit transaction (auto rollback on failure)
If m_DB.TransCommit Then
    Debug.Print "Success"
Else
    Debug.Print "Failed: " & m_DB.LastErr
End If

Q5: How to Prevent SQL Injection

vb
' Wrong: Direct SQL concatenation (unsafe)
m_DB.Sql("SELECT * FROM users WHERE name = '" & txtName.Text & "'").Query

' Correct: Use parameterized query (safe)
m_DB.Sql("SELECT * FROM users WHERE name = ?") _
    .Param("name", txtName.Text, VBMAN.adVarWChar) _
    .QueryParam

Q6: How to Get Last Inserted ID

vb
' Insert data
m_DB.Sql("INSERT INTO users (name) VALUES (?)") _
    .Param("name", "John", VBMAN.adVarWChar) _
    .ExecParam

' Get last inserted ID
Dim lId As Variant
lId = m_DB.LastInsertId
Debug.Print "New user ID: " & lId

Next Steps


Last Updated: 2026-01-21

VB6 and LOGO copyright of Microsoft Corporation