Coffeehouse Post

Single Post Permalink

View Thread: VBA Horror Stories! Tell us about the sin ...
  • User profile image
    Manip

    If you want to produce good quality code in VBA do the following:

    - Option Explicit (From the start!)
    - NO ERROR CONTROL! (Do not use error control to avoid making things correctly..)
    - Goto is banned.

    If you follow these simple instructions you've already got a fairly good standard of code.. then just write.. optimize... test... write... optimize...

    Look at this:

    Open sz For Input As #7
    ERRLINS = 0
    Do
     THISLIN$ = "**END**"
     On Error Resume Next
     Line Input #7, THISLIN$
     If THISLIN$ = "**END**" Then Exit Do
     If THISLIN$ <> "" Then
      'Code.....
      '......
     Else
      ERRLINS = ERRLINS + 1
     End If
    Loop

    If ERRLINS > 0 Then IMPERR$ = " (Import errors: " & ERRLINS & ")"
    Label1 = "Status: IDLE  (" & ListView1.ListItems.Count & " ranges)" & IMPERR$


    Some of the variables where defined.. but worse is the use of error control to handle the EOF! Here is how it SHOULD have been done:

    Dim FreeStream%, THISLIN$
    FreeStream = FreeFile
    Open sz For Input As FreeStream

    Do while NOT EOF(FreeStream)
     Line Input #FreeStream, THISLIN$
     If NOT THISLIN = "" Then
      'Code.....
      '......
     End If
    Loop

    Label1.caption = "Status: IDLE  (" & ListView1.ListItems.Count & " ranges)"

    This is before any error control (non-release). The code fragment is from a semi-popular application that a couple of Channel 9 users are probably running.. that is how poor some VBA coders program.