vbdepot.com

The code depot · 09

Classic Snippets That Still Teach

Every language leaves behind idioms the way a workshop leaves behind jigs. These are the classic VB patterns: annotated, colour-coded like the old IDE, and still worth an afternoon of your attention.

VB Depot editorsUpdated Aug 20267 min read
\VBDEPOT\SNIPPETS\DEFAULT.HTM

The depot's position on legacy code is that it is literature: reading it teaches you how people thought, what they feared, and what their tools made easy. Classic Visual Basic, the dialect of VB6 and its lifelong companion VBA, reads today like a very practical school of prose. Its habits were shaped by the constraints described in the RAD workspace tour: short procedures, defensive error handling, and a strong preference for the obvious over the clever.

1. The hello world, complete

FORM1.FRM
' The canonical first program (an event), a dialog, done.
Option Explicit

Private Sub Command1_Click()
    Dim userName As String
    userName = Trim(InputBox("Your name?", "VB Depot"))
    If Len(userName) = 0 Then Exit Sub
    MsgBox "Hello, " & userName & "!", vbInformation, "Welcome"
End Sub

Everything the language stood for is in these seven lines: behaviour attached to an event, a defensively trimmed input, an early exit instead of a nested branch, and a completed interaction with no ceremony. Note Option Explicit at the top, off by default in fresh VB6 projects, on by discipline in every professional codebase.

2. Error handling with cleanup, the real pattern

MODUTILS.BAS
Private Sub ProcessFile(ByVal path As String)
    On Error GoTo Fail
    Dim fnum As Integer
    fnum = FreeFile
    Open path For Input As #fnum

    Do While Not EOF(fnum)
        Dim line As String
        Line Input #fnum, line
        ' ... handle the line ...
    Loop

Tidy:
    On Error Resume Next
    Close #fnum
    Exit Sub

Fail:
    MsgBox "Could not process file: " & Err.Description
    Resume Tidy
End Sub

On Error GoTo was much mocked, mostly by people who never read the second half of the pattern. The mature idiom pairs a handler with a cleanup section that the success path falls through and the failure path resumes into: an honest ancestor of try/finally, arrived at from the other direction. The goto was the flaw; the discipline was the point.

3. Driving Excel from the outside: the automation idiom

REPORTGEN.BAS
' Late binding: works with any installed Office version.
Dim xl As Object, wb As Object
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Add
xl.Visible = False

wb.Worksheets(1).Range("A1").Value = "Quarterly report"
wb.Worksheets(1).Range("A1").Font.Bold = True

wb.SaveAs App.path & "\report.xls"
wb.Close False
xl.Quit
Set wb = Nothing: Set xl = Nothing

The COM automation handshake, create, drive, close, quit, then release, anchored a thousand reporting systems and half of the VB6-to-Office bridge described in the VBA deep dive. The colon-joined Set … = Nothing pair at the end was superstition for most objects and vital practice for a few; every veteran could name the few.

4. For Each, the friendly loop

FORMTOUR.BAS
Dim ctl As Control
For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
        ctl.Text = vbNullString
    End If
Next ctl

A form is a collection; the collection is enumerable; clearing every text box is four lines. This gentle treatment of containers is one of the design strains that survived intact into VB.NET, see the Great Split for what changed and what carried over, and into the daily practice of anyone who has ever written a loop over a spreadsheet's cells.

DEPOT NOTE

These snippets are idiomatic, not aspirational, modern VBA style adds naming conventions and modules, and twinBASIC (covered in the community guide) modernises the toolchain wholesale. If you maintain a live codebase of this vintage, the practical playbook is Migrating VB6 Applications Today.