FIX: Forms.Count and Controls.Count Collections Cause Error

ID: Q180573


The information in this article applies to:


SYMPTOMS

When accessing the Count property of either the Forms or the Controls collection, the following error is generated:

Error 450: "Wrong number of arguments or invalid property assignment"


RESOLUTION

The following code sample shows how to use the two functions below to get the Forms Count and Controls Count values without producing an error.

  1. Create a new Windows CE Project in Visual Basic 5.0. Form1 is created by default.


  2. Add a CommandButton to Form1.


  3. Add the following code to Form1:
    
          Option Explicit
    
          Private Sub Command1_Click()
              'MsgBox Forms.Count and Controls.Count generate an error,
              'so use the following Functions instead.
              MsgBox "There are/is " & FormsCount & " form(s) loaded"
              MsgBox "Form(0) contains " & ControlsCount & " control(s)"
          End Sub
    
          Private Function FormsCount()
              '===========================================
              'Work around for Forms.Count bug.
              'Returns the number of loaded forms.
              '===========================================
              Dim iFormCount
              Dim sDummy
              On Error Resume Next
              Do
                  sDummy = Forms(iFormCount).Name
                  If Err.Number <> 0 Then
                      FormsCount = iFormCount
                      Exit Do
                  End If
                  iFormCount = iFormCount + 1
              Loop
          End Function
    
          Private Function ControlsCount()
              '===============================================
              'Work around for Controls.Count bug.
              'Returns the number of Controls on the form.
              '===============================================
              Dim iControlsCount
              Dim sDummy
              On Error Resume Next
              Do
                  sDummy = Controls(iControlsCount).Name
                  If Err.Number <> 0 Then
                      ControlsCount = iControlsCount
                      Exit Do
                  End If
                  iControlsCount = iControlsCount + 1
              Loop
          End Function 


  4. Press the F5 key to run the project.



STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

This problem was corrected in Windows CE Toolkit for Visual Basic 6.0.


MORE INFORMATION

Steps to Reproduce Behavior

WARNING: The following steps will cause an exception error to occur.

  1. Create a new Windows CE Project in Visual Basic 5.0. Form1 is created by default.


  2. Add a CommandButton to Form1.


  3. Add the following code to Form1:
    
          Private Sub Command1_Click()
              'MsgBox Forms.Count and Controls.Count generate
              'an error as shown in the following code:
              MsgBox "There are/is " & Forms.Count & " form(s) loaded"
              MsgBox "Form(0) contains " & Controls.Count & " control(s)"
          End Sub 


  4. Press the F5 key to run the project.


© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Mike Dixon, Microsoft Corporation

Additional query words: wince vbce vbce5 vbce6


Keywords          : kbToolkit kbVBp kbVBp500bug kbVBp600fix kbWinCE kbWinCE100 kbGrpVB 
Version           : WINDOWS:1.0
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: February 25, 1999