Procedures

In procedure naming, the key things to indicate are the procedure's type (function or sub), and its scope (global or modular). These help the programmer determine, at a glance, whether to expect the procedure to return a value, and where to look for the procedure code. The scope portion comes first, then the procedure type prefix, as the following table shows:

Table 8: Naming Procedures

Prefix

Procedure Type

Example

gf

Global function

gfNavigate, gfUpdateControls

gs

Global sub

gsPopulateGrid

mf

Form function

mfOrdersCalculate

ms

Form sub

msCurrentCustomer


With all of these naming conventions in mind, examine the following code listing. Notice how it illustrates many of the points made in the previous tables: procedure and argument naming, control and object naming, local data variables and so forth.

Listing 2: Applying Naming Conventions in Code


Sub gsPopulateGrid (rgrdCurrent As Grid, rrssCurrent As Snapshot)
    
    On Error GoTo PopulateGrid_Error
    Dim liRowCount As Integer
    Dim liColCount As Integer
    Dim liNumCols As Integer
    Dim liCounter As Integer
    
    liRowCount = 0
    liNumCols = rrssCurrent.Fields.Count
    rgrdCurrent.Cols = liNumCols
    'Loop to put field names on column heads
    rgrdCurrent.Row = 0
    For liColCount = 0 To liNumCols - 1
        rgrdCurrent.Col = liColCount
        rgrdCurrent.Text = rrssCurrent.Fields(liColCount).Name
    Next liColCount
    'Loop through rows of snapshot
    Do Until rrssCurrent.EOF
        liRowCount = liRowCount + 1
        rgrdCurrent.Row = liRowCount
        
        'Loop through fields within a row
        For liColCount = 0 To liNumCols - 1
            rgrdCurrent.Col = liColCount
            rgrdCurrent.Text = IIf(IsNull(rrssCurrent.Fields(liColCount)), " ", rrssCurrent.Fields(liColCount))
            'rgrdCurrent.ColWidth(liColCount) = rrssCurrent.Fields(liColCount).Size * 150
            rgrdCurrent.ColWidth(liColCount) = 1000
        Next liColCount
        rgrdCurrent.Rows = rgrdCurrent.Rows + 1
        rrssCurrent.MoveNext
    Loop
    Exit Sub
PopulateGrid_Error:
    MsgBox "A problem occurred when trying to populate grid. " & Chr(10) & Chr(13) & "Error number: " & CStr(Err) & " -- Error message: " & Error(Err)
    rgrdCurrent.Visible = False
    On Error GoTo 0
    Exit Sub
End Sub