Passing control array into global function - problem 
Author Message
 Passing control array into global function - problem

Mark,

One way to accept a control array as as argument to a sub or function is to
use a Variant data type:

Private Function getChoice(myOptions As Variant) As Long

    Dim counter As Long

    For counter = myOptions.Lbound To myOptions.Ubound
        If myOptions(counter).Value = True Then
            getChoice = counter
        End If
    Next counter

End Function

To call this function, sending it a control array of option buttons:

    Dim response As Long

    response = getChoice(optChoice())
    MsgBox response

Note that the parentheses after optChoice are optional.

There is probably a more straigtforward way to accomplish what you are
trying to do. I would be interested to see other replies.

Kerry Moorman


Quote:
>Grateful for help please

>I have 3 option buttons on a form as an array ,opt_Choice() with  index 1,2
>and 3 (NO zero index)
>The process command button click event calls the global function
>gf_WhatIndex which is supposed to return the index of the chosen option
>button

>I am getting a type mismatch error

>Is it possible to pass a control array like this to a global variable?
>Is it a parenthesis problem?
>Is a control array ( ie opt_Choice(1)  (2)  and  (3)   ) a proper array if
>it is created at design time?

>This is the form code
>--------------------------------------------------
>Private Sub cmd_Process_Click()
>Dim li_IndexResult As Integer
>Call gf_WhatIndex(opt_Choice(), opt_Choice.Count, li_IndexResult)
>txt_Display.Text = Str(li_IndexResult)
>End Sub
>-----------------------------------------------
>This is the module code
>-----------------------------------------------
>Public Function gf_WhatIndex(ar_Control() As Control, ar_ConrolCount As
>Integer, ar_IndexResult as Integer) As Integer
>Dim li_loop As Integer
>    For li_loop = 1 To ar_ControlCount
>        If ar_Control(li_loop).Item = True Then ' index of this control is
>set
>            ar_IndexResult = li_loop
>        End If
>    Next li_loop
>End Function
>--------------------------------------------
>Thanks in advance

>btw I am just a hobby programmer and I know this could be done
differently -
>its just i am trying to get used to writing generic global functions that I
>can reuse etc...



Sat, 22 Dec 2001 03:00:00 GMT  
 Passing control array into global function - problem
Mark,

As you may know, the normal way to deal with option buttons is to use their
click event to keep track of their current index, e.g.

Private Sub opt_Choice_Click(Index As Integer)
    txt_Display.Text = Index
    ' or assign index to variable for later reference
End Sub

Kerry gave you what you need to be able to do it the hard way.  You would do
well to note not only the use of the variant for passing the control array, but
the way that Kerry stripped out the unnecessary parameters and the way he called
the function.  This is a good model to follow for other functions you may wish
to define.

John.....

Quote:

> Grateful for help please

> I have 3 option buttons on a form as an array ,opt_Choice() with  index 1,2
> and 3 (NO zero index)
> The process command button click event calls the global function
> gf_WhatIndex which is supposed to return the index of the chosen option
> button

> I am getting a type mismatch error

> Is it possible to pass a control array like this to a global variable?
> Is it a parenthesis problem?
> Is a control array ( ie opt_Choice(1)  (2)  and  (3)   ) a proper array if
> it is created at design time?

> This is the form code
> --------------------------------------------------
> Private Sub cmd_Process_Click()
> Dim li_IndexResult As Integer
> Call gf_WhatIndex(opt_Choice(), opt_Choice.Count, li_IndexResult)
> txt_Display.Text = Str(li_IndexResult)
> End Sub
> -----------------------------------------------
> This is the module code
> -----------------------------------------------
> Public Function gf_WhatIndex(ar_Control() As Control, ar_ConrolCount As
> Integer, ar_IndexResult as Integer) As Integer
> Dim li_loop As Integer
>     For li_loop = 1 To ar_ControlCount
>         If ar_Control(li_loop).Item = True Then ' index of this control is
> set
>             ar_IndexResult = li_loop
>         End If
>     Next li_loop
> End Function
> --------------------------------------------
> Thanks in advance

> btw I am just a hobby programmer and I know this could be done differently -
> its just i am trying to get used to writing generic global functions that I
> can reuse etc...



Sat, 22 Dec 2001 03:00:00 GMT  
 Passing control array into global function - problem
One way is to use the type of the control instead of "as control" in the
call. Something like:
Public Function gf_WhatIndex(ar_Control() As ListBox, ar_ConrolCount As

Quote:
>Integer, ar_IndexResult as Integer) As Integer



Quote:
>Grateful for help please

>I have 3 option buttons on a form as an array ,opt_Choice() with  index 1,2
>and 3 (NO zero index)
>The process command button click event calls the global function
>gf_WhatIndex which is supposed to return the index of the chosen option
>button

>I am getting a type mismatch error

>Is it possible to pass a control array like this to a global variable?
>Is it a parenthesis problem?
>Is a control array ( ie opt_Choice(1)  (2)  and  (3)   ) a proper array if
>it is created at design time?

>This is the form code
>--------------------------------------------------
>Private Sub cmd_Process_Click()
>Dim li_IndexResult As Integer
>Call gf_WhatIndex(opt_Choice(), opt_Choice.Count, li_IndexResult)
>txt_Display.Text = Str(li_IndexResult)
>End Sub
>-----------------------------------------------
>This is the module code
>-----------------------------------------------
>Public Function gf_WhatIndex(ar_Control() As Control, ar_ConrolCount As
>Integer, ar_IndexResult as Integer) As Integer
>Dim li_loop As Integer
>    For li_loop = 1 To ar_ControlCount
>        If ar_Control(li_loop).Item = True Then ' index of this control is
>set
>            ar_IndexResult = li_loop
>        End If
>    Next li_loop
>End Function
>--------------------------------------------
>Thanks in advance

>btw I am just a hobby programmer and I know this could be done
differently -
>its just i am trying to get used to writing generic global functions that I
>can reuse etc...



Sat, 22 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing Arrays to Functions - Subscript Out of Range Problem

2. passing a control array to a function...

3. Passing control arrays into functions - how?

4. how do i pass control array to a function

5. Pass a control array to a function?

6. pass control array to function

7. How to pass a control array to a procedure or function

8. can a control array be passed to a function

9. Passing in a control array to a Function??

10. Pass a control array to a function?

11. Passing Control Arrays to a function???

12. Passing a Control Array to a Function

 

 
Powered by phpBB® Forum Software