Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to Pass a Control Group as a Procedure Parameter

Posted on 2005-02-26
Medium Priority
Last Modified: 2010-05-02
I have a group of picture controls and command buttons on a form.  I need to pass the control groups to a Sub or Function.

This is my code:

ScanGroup me.picCards(), me.cmdPickOne

Sub ScanGroup(ppicCards() as PictureBox, pcmdPickOne() as Control)

Dim intCtr1 as integer

For intCtr1 to ppicCards.Count - 1
      If ppicCards(intctr) = pintCritical Then  
                  'Do something
                  Exit For
     End If
Next intCtr1

For intCtr1 to pcmdPickOne.Count - 1
     If pcmdPickOne(intctr) = pintCritical Then  
                  'Do something
                  Exit For
     End If
Next intCtr1

When I try to compile, I get a type mismatch error.  This comes from an incorrect parameters statement.

I can pass arrays with no problem.
What is different about control groups?
What am I doing wrong ?


Question by:Dovberman
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 13411185
(There are typos in your code.  I highly recommend you put Option Explicit at the top of your code.)

Anyhoo, pass the control arrays as Object:

Private Sub Command1_Click()
    ScanGroup Me.picCards, Me.cmdPickOne
End Sub

Sub ScanGroup(ppicCards As Object, pcmdPickOne As Object)
    Dim intCtr1 As Integer
    For intCtr1 = 0 To ppicCards.ubound
        If ppicCards(intCtr1) = pintCritical Then
            'Do something
            Exit For
        End If
    Next intCtr1

    For intCtr1 = 0 To pcmdPickOne.ubound
        If pcmdPickOne(intCtr1) = pintCritical Then
            'Do something
            Exit For
        End If
    Next intCtr1
End Sub

Author Comment

ID: 13412012
Your solution works fine.  The key was to refer to the control and picture box as objects.

I do use Option Explicit in all my code. Not shown in the snippet.

Thanks for the prompt reply.


Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question