Answered VB 7 Looping thru Controls

  • Monday, April 09, 2012 6:36 PM
     
     

    I am running Win 7 (64-bit) with Office 2010 (VB 7).

    I have some code that loops thru all controls in a form and does some stuff. 

    This was working fine in XP with Office 2007. 

    This code now throws type mismatch error at the For each.... line. 

        For Each ctlControl In Controls
            If TypeName(ctlControl) = "CheckBox" Then
                If ctlControl.Name <> chkAll.Name Then
                    If ctlControl.Value = True Then
                        Call doProc(ctlControl.Caption)
                    End If
                End If
            End If
        Next ctlControl

    I have not included any dependencies except remove DAO and added Microsoft Access 14.0 Object Library.

    I am at a loss to understand why this would not work. Any help would be greatly appreciated. 

    Thanks.

All Replies

  • Monday, April 09, 2012 6:44 PM
     
     Proposed Answer

    This forum is about the .Net based standalone versions of Visual Basic. Please have a look at this forum instead:
    http://social.msdn.microsoft.com/Forums/en-US/worddev/threads

    (I think there you should also mention how ctlControl is declared. If it's CheckBox, if fails if there are not only checkboxes on the Form)

    EDIT: Sorry, doesn't have to be Word. Can be any other Office product, too, but I think you find it from the given link.


    Armin


  • Monday, April 09, 2012 6:53 PM
     
      Has Code

    If you were looking for a .Net solution:

            For Each FormControl As Control In Me.Controls
                If TypeOf (FormControl) Is CheckBox Then
                    If Not (FormControl.Name = chkAll.Name) Then
                        If DirectCast(FormControl, CheckBox).Checked = True Then
                            'Do Your Stuff Here
                        End If
                    End If
                End If
            Next


  • Monday, April 09, 2012 6:59 PM
     
     

    I am running Win 7 (64-bit) with Office 2010 (VB 7).

    I have some code that loops thru all controls in a form and does some stuff. 

    This was working fine in XP with Office 2007. 

    This code now throws type mismatch error at the For each.... line. 

    Dim ctlControl As Control

        For Each ctlControl In Controls
            If TypeName(ctlControl) = "CheckBox" Then
                If ctlControl.Name <> chkAll.Name Then
                    If ctlControl.Value = True Then
                        Call doProc(ctlControl.Caption)
                    End If
                End If
            End If
        Next ctlControl

    I have not included any dependencies except remove DAO and added Microsoft Access 14.0 Object Library.

    I am at a loss to understand why this would not work. Any help would be greatly appreciated. 

    Thanks.

  • Monday, April 09, 2012 6:59 PM
     
     
    Thanks, I posted it in the right place. Sorry I did not realize it.
  • Monday, April 09, 2012 7:00 PM
     
     

    Thanks, it is not .NET.

    I had posted in the wrong forum. My bad.

  • Monday, April 09, 2012 7:36 PM
     
     Answered Has Code

    The declaration ... As Control has become ambiguous because Control is a Forms object, but also an Access object.

    To remove the ambiguity, declare ctlControl as follows:

    Dim ctlControl As MSForms.Control


    Regards, Hans Vogelaar

    • Proposed As Answer by Mark Liu-lxfModerator Wednesday, April 11, 2012 5:37 AM
    • Marked As Answer by Tinausa Wednesday, April 11, 2012 10:45 AM
    •  
  • Tuesday, April 10, 2012 5:19 PM
     
     
    Thanks Hans that worked.