PRB: "Invalid Procedure Call" Error with Preserve Keyword

ID: Q193092

The information in this article applies to:

SYMPTOMS

The following error occurs if a variant set equal to an Array() function is redimensioned using the Preserve keyword:

   Run-time error '5' "Invalid Procedure Call or Argument"

NOTE: This behavior occurs only under the following two conditions:

      - If Microsoft Data Access Components (MDAC) 2.0 or Visual Studio 6.0
        is installed on the computer.

        -and-

      - If the array has not been initialized.

CAUSE

This error occurs with the newer version of the OLE automation core file, (version 2.304261). The Oleaut32.dll file is installed in your system directory when you install MDAC 2.0 or Visual Studio 6.0 products.

RESOLUTION

Here are two ways to resolve this problem:

Method One

NOTE: The preceding workaround can only be used with a Visual Basic 5.0 application running with MDAC 2.0 installed on the computer. The newer version of the automation file, Oleaut32.dll (version 2.304261), must be present on a computer that runs a Visual Basic 6.0 application.

WARNING: USE THE WORKAROUND PROVIDED ABOVE AT YOUR OWN RISK. Microsoft provides this workaround "as is" without warranty of any kind. If you choose to implement the above workaround please make sure that you match other versions of OLE files on your system.

Method Two

You can choose to keep the newer version of the Oleaut32.dll file. However, you cannot preserve your array unless it was initialized prior to the call. For example:

      Sub Command1_Click ()
          Dim VarArr as Variant
          Dim lngSubScript As Long

          VarArr = Array()

          ' Get largest available subscript for the array.
          lngSubScript = UBound(A)

          If (lngSubScript <> -1) Then
             ' Re-allocate 5 elements of the array and preserve its values.
             ReDim Preserve VarArr(5)
          Else
             ' Re-allocate 5 elements of the array.
             ReDim VarArr(5)
          End If
      End Sub

NOTE: The preceding workaround applies to both Visual Basic 5.0 applications and Visual Basic 6.0 applications running with the newer version of the OLE automation file.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

1. Start a new project in Visual Basic and choose "Standard EXE". Form1 is

   created by default.

2. Add a Command button, named Command1, to Form1.

3. Paste the following code in the General Declaration section of Form1:

      Option Explicit
      Dim VarArr As Variant

      Private Sub Command1_Click()

         ' Get the variant to hold an array.
         VarArr = Array()

         ' Re-allocate 5 elements of the array and preserve its values.
         ReDim Preserve VarArr(5)         '<--- Error 5 occurs here

      End Sub

4. Run the application or press the F5 key. Click the Command1 button.
   The "Error 5" occurs.

REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q187282
   TITLE     : INFO: List of Visual Basic Run-Time Files Installed by
               Product

   ARTICLE-ID: Q190130
   TITLE     : INFO: Description of VB 6.0 Run Time and OLE Automation
               Files

Additional query words: kbDASDKInstall kbVBp600 kbADO200
Keywords          : kbdasdkInstall 
Version           : WINDOWS:2.0,6.0
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: September 29, 1998