ICI logo

Intersystem Concepts, Inc.

VB6 Error 10 Fix

Your project runs fine in the IDE, so you compile it into an EXE, run it, and get the following message:

The message occurs upon an attempt to ReDim an array.

There are several possible causes, at least one of which is obscure. A more obvious case involves using Redim on a Public array within a nested (recurive) iteration of a procedure. Such Redim is usable only within the first iteration of that procedure.

The obscure case we encountered involved improper use of an array element inside a nested With block.

The following sample code snippet (from a routine that employs the TextOut API to display rotated text) illustrates the situation:

  Dim Log_Font As logfonttype
  With arr(index)
    With Log_Font
      .lfEscapement = .frotation         $$ HERE'S THE TROUBLE
      .lfHeight = -24 
      .lfFaceName = Me.FontName & vbNullChar
      .lfWeight = 400
    End With
    lfontnew& = CreateFontIndirect(Log_Font)
    lfontold& = SelectObject(Me.hDC, lfontnew&)
    If TextOut&(Me.hDC, 0, 0, .stext, Len(.stext)) = 0 Then
      Debug.Print "TextOut Error"
    End If
    Call SelectObject(Me.hDC, lfontold&)
    Call DeleteObject(lfontnew&)
  End With
The fourth line of code above should instead read:
      .lfEscapement = arr(index).frotation       
but due to the nested With blocks, the problem is easy to miss, and the compiler does not trap it. As an .EXE, VB6 ties the array element .frotation to the TextOut object, and this results in confusion (Error 10) later when the array is redimensioned. Change the example as shown and the problem goes away.
Other VB tips