DOCUMENT:Q194617 03-NOV-1998 [vbwin] TITLE :FIX: ReDim Preserve Fails on Uninitialized Arrays PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:6.0 OPER/SYS:WINDOWS KEYWORDS: ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0 --------------------------------------------------------------------- SYMPTOMS ======== After you've used Redim Preserve to re-dimension a dynamic array and run the application, it terminates with the following error: "Runtime Error 5: "Invalid Procedure Call or Argument". CAUSE ===== You cannot use Redim Preserve on a dynamic array that has not been initialized. RESOLUTION ========== The workaround is to check the value of the upper bound of the array and , if it is not initialized (the upper bound will have a value of -1), use a Redim statement without Preserve. Below is the sample code that demonstrates this workaround: a = Array() b = UBound(a) 'get value of largest available array subscript If (b <> -1) Then '-1 is the return value of a uninitialize array ReDim Preserve a(5) Else ReDim a(5) 'no elements exist in array use ReDim End If STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0, Service Pack 1. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- 1. Create a Standard EXE project in Visual Basic. Form1 is created by default. 2. Place a CommandButton on Form1 and paste the following code: Private Sub Command1_Click() Dim a As Variant a = Array() ReDim Preserve a(5) End Sub 3. Press the F5 key to run the application, and note that the above error appears. Additional query words: kbDSupport kbVBp600 kbVS600SP1fix kbNoKeyWord ====================================================================== Version : WINDOWS:6.0 Platform : WINDOWS Issue type : kbbug Solution Type : kbfix ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.