VB 6 Coding Style

In general, it’s more important to have some coding styles than to use a particular set of rules. Code that is consistent is easier to read, understand, debug, and maintain no matter what rules you use.

However, it amazes me how many developers seem to have no rules or rules that are inconsistent and contradictory. In one very large (56,000 line) project, I’ve seen people using arbitrary indentation, no comments, improper variable naming (for example, using an “m” prefix that normally means “module-level” for local variables), and so forth.

So here are the rules that I generally use. Adopt the ones that makes sense to you and replace the others. Note that these are the rules that I use for Visual Basic 6. I use slightly different rules for other languages, including VB .NET.


  • Indentation: Indent 4 spaces to show scope. Indent within a subroutine, loop, or If Then block as in the following:
        Private Sub MySub()
        Dim i As Integer
            For i = 1 To 10
                Debug.Print i
            Next i
        End Sub
  • Make sure indentation lines up block start and end statements. For example, make sure the If, Then, Else, and End If statements in a block all line up.
  • Declare variables at the beginning of a subroutine at the same level of indentation as the Sub statement. (A lot of people prefer indent the declarations. Whatever you prefer. Just pick one. Note that VB .NET indents them for you whether you like ot or not.)
  • Use Option Explicit and Option Strict.
  • Declare each variable on a separate line.
  • Prefix module-level variables with “m_” and use “Pascal Case” (capitalize the first letter of each word) as in m_NumEmployees.
  • Prefix global-level variables with “g_” and use “Pascal Case” as in g_NumEmployees.
  • Name constants in ALL CAPS with underscores as in MAX_EMPLOYEES.
  • Specify a data type for all variables and constants as in:
        Private Const MAX_EMPLOYEES As Long = 100
  • Name routine-local variables in lower case with underscores as in num_employees.
  • Use Pascal case for subroutine, function, property, and other routine names as in NumEmployees().
  • Always specify one of Private, Public, etc. whenever those are allowed. I.e. don’t declare a subroutine with just Sub, use Private or Public to make its scope obvious.
  • Use type prefixes on control names as in picEmployee (a PictureBox) and lblResult (a Label).
  • Do not use type prefixes (aka Hungarian notation) for other variables.
  • Make variable names obvious enough that you don’t miss Hungarian notation.
  • Use a comment at the beginning of every module explaining what its purpose is.
  • Use a comment before every routine explaining what the routine does. Explain parameters and return values.
  • Use liberal comments within the code to explain what it is doing.
  • Do not use GoTo.
  • Do not use IIF. It’s confusing and If Then Else is actually faster.
  • Do not use the : character to place more than one statement on a single line.
  • If a single-line If Then statement is long, make it a multi-line If Then End If statement so it’s easier to read.
  • Don’t try to make anything do too many things. For example, Visual Basic’s Line command draws lines or rectangles, optionally specifying the drawing color and whether the rectangle should be filled. This should have been at least two routines, Line and Rectangle.
  • When you use error handling code with an On Error GoTo statement, always place an Exit Sub statement immediately before the error handler. Never allow the code to drop into the error handling code. This makes the code run in a special error handling mode and can be quite confusing.
  • Use Debug.Assert to test for conditions that should not occur. This lets you catch bugs early and easily.
  • If you catch an error, check for all errors not just the ones you expect. Use Debug.Assert to catch those you don’t expect.
  • In a Select Case statement, use a Case Else to catch unexpected conditions. If you don’t think the condition should ever occur, use Debug.Assert to detect it.

Masalah ieframe.dll

Pada saat kita menggunakan control internet control pada VB6 tidak 100% berjalan mulus. Ketika komputer diinstall browser dengan IE versi >IE6 maka akan menemui pesan error berikut “File Not found ‘C:\Windows\system32\ieframe.dll\1′” Solusi nya adalah dengan mencari registry dengan kata kunci ‘C:\Windows\system32\ieframe.dll\1’, dengan menggunakan tool seperti tuneUp 2009, alamat registrynya adalah HKEY_LOCAL_MACHINE\SOFTWARE\Classes\TypeLib\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\1.1\win32. dan mengganti text ‘C:\Windows\system32\ieframe.dll\1’ menjadi ‘C:\Windows\system32\ieframe.dll’

Automate Excel via VB

Cara membuat file excel melalui VB 6.0:

Private Sub Command1_Click()
    On Error GoTo errH
    Dim oExcel As Object
    Dim oWB As Object
    Dim oWS As Object
    Dim oRng As Object

    Set oExcel = CreateObject("Excel.Application")
    Set oWB = oExcel.Workbooks.Add
    Set oWS = oWB.Worksheets("Sheet1")
    Set oRng = oWS.Range("A1")
    oRng.Value = "Hello World"

    'oWS.Cells(1, 1).Value
    oWB.SaveAs ("c:\Hello World.xls")

    GoTo Cleanup
    If Err.Number = 9 Then 'jika error pada pembukaan worksheet
        MsgBox "Tidak dapat membuka worksheet, mungkin nama worksheet salah", vbCritical, "Error"
        MsgBox Err.Description, vbCritical, "Error"
    End If
    Set oWS = Nothing
    On Error Resume Next
    If Not oWB Is Nothing Then oWB.Close
    Set oWB = Nothing
    Set oExcel = Nothing
    On Error GoTo 0
End Sub

Disable Pop Up menu From Textbox

A common Frequently Asked Question is how to disable the popup menu of a TextBox. Since VB5 was released it’s possible to use the AdressOf keyword to add a Hook and a callback function. Just add this code to a .BAS module and call the Hook sub and pass the hWnd of a textbox as an argument. You must call the UnHook sub before you unload the form or you might get a General Protection

pertanyaan yang sering ditanyakan adalah bagaimana men-disable menu popup yang ada pada Textbox, berikut ini adalah contoh program kecil untuk men-disable menu popup tersebut:

buat kode berikut pada deklarasi modul:

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, _
    ByVal hWnd As Long, _
    ByVal Msg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" _
    (ByVal hWnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Private lngHWnd As Long

Buat kode berikut pada modul:

Public Sub Hook(hWnd As Long)
    lngHWnd = hWnd
    lpPrevWndProc = SetWindowLong(lngHWnd, GWL_WNDPROC, _
    AddressOf WindowProc)
End Sub

Public Sub UnHook()
    Dim lngReturnValue As Long
    lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_RBUTTONUP
            'Do nothing
            'Or popup you own menu
        Case Else
            WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
    End Select
End Function

Buat kode berikut, letakkan pada event Form_Load() dimana Textbox berada:

Call Hook(Text1.hWnd)

Enumerasi Printer

Pada kasus-kasus tertentu mungkin kita perlu mengetahui daftar atau list printer yang ada, dan memilih nya sebagai printer yang aktif. Untuk lebih jelasnya perhatikan contoh berikut ini:
Baca lebih lanjut

Systray Menyembunyikan Aplikasi

Pada artikel ini akan di jelaskan tentang bagaimana cara membuat aplikasi yang jalan menjadi systray, langsung aja ke contoh kode nya ya., gak pake basa-basi:
Baca lebih lanjut

Direct Print Ke Epson LX300 Dengan VB6

Dapet dari blog sebelah, kebetulan aku memiliki masalah yang sama dalam mencetak dokumen langsung ke printer Epson LX300 untuk dokumen, sebelumnya sih aku pake metode print melalui driver tapi hal ini kurang cocok untuk nota struk POS
Baca lebih lanjut