469,148 Members | 1,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,148 developers. It's quick & easy.

How do I pass the name of an array to a function or procedure?

Hey i've got bunch of arrays of tick boxes, each array contains somewhere
between 5 and 20.

What I want to do is write a function that returns the captions of every
ticked tick box in an array as a string.

I want to be able to pass to the function the array name, and the lower and
upper limits of the array. But I have no idea how to pass the control name
(array name).

I can do it by writing a prodecure for every single array, but there are
hundreds of them! It must be possible using a single function or procedure.
I came up with this, it needs finishing.

Private Function ReturnString(ctrlName As ***WHAT?????**** , intLow As
Integer, intHigh As Integer) As String
intFlag = 0
strField = ""
For intCount = intLow To intHigh
If ctrlName(intCount).Value = 1 Then
If intFlag = 0 Then
strField = ctrlName(intCount).Caption
intFlag = 1
Else
strField = strField & ", " & ctrlName(intCount).Caption
End If
End If
Next intCount
ReturnString = strField
End Function
Thanks for any help

Steve
Jul 17 '05 #1
4 2713
> Hey i've got bunch of arrays of tick boxes, each array contains
somewhere
between 5 and 20.

What I want to do is write a function that returns the captions of every ticked tick box in an array as a string.

I want to be able to pass to the function the array name, and the lower and upper limits of the array. But I have no idea how to pass the control name (array name).

I can do it by writing a prodecure for every single array, but there are hundreds of them! It must be possible using a single function or procedure. I came up with this, it needs finishing.

Private Function ReturnString(ctrlName As ***WHAT?????**** , intLow As
Integer, intHigh As Integer) As String
intFlag = 0
strField = ""
For intCount = intLow To intHigh
If ctrlName(intCount).Value = 1 Then
If intFlag = 0 Then
strField = ctrlName(intCount).Caption
intFlag = 1
Else
strField = strField & ", " & ctrlName(intCount).Caption
End If
End If
Next intCount
ReturnString = strField
End Function


Three points. First, declare the ctrName argument as Object. Second, Dim
all of your variables. Right now, every variable in your Function is a
Variant. Variants execute slower than declared variables and they take
up more memory. Third, you don't need to pass the lower and higher
indexes of the control array because VB will already knows them. Inside
of the Function, you could set your For-Next loop like this (and
eliminate intLow and intHigh altogether).

For intCount = ctrlName.LBound to ctrlName.UBound

HOWEVER, there is an even better way to handle this... use a For Each
loop. You will need to

Dim ctrl As Control

and then change your loop to this...

For Each ctrl In ctrlName
If ctrl.Value = 1 Then
If intFlag = 0 Then
strField = ctrl.Caption
intFlag = 1
Else
strField = strField & ", " & ctrl.Caption
End If
End If
Next

Rick - MVP

Jul 17 '05 #2
On Mon, 26 Jul 2004 18:34:35 +1200, "Stephen Williams"
<st*****@hotmail.com> wrote:
Hey i've got bunch of arrays of tick boxes, each array contains somewhere
between 5 and 20.

What I want to do is write a function that returns the captions of every
ticked tick box in an array as a string.

I want to be able to pass to the function the array name, and the lower and
upper limits of the array. But I have no idea how to pass the control name
(array name).

I can do it by writing a prodecure for every single array, but there are
hundreds of them! It must be possible using a single function or procedure.
I came up with this, it needs finishing.


Option Explicit

Private Sub Command1_Click()
Call LS_SetCheckBoxes(Check1)
End Sub

Sub LS_SetCheckBoxes(Check1 As Object)
Dim C As CheckBox

For Each C In Check1
Select Case C.Value
Case vbChecked: C.Value = vbUnchecked
Case vbUnchecked: C.Value = vbChecked
End Select
Next
' --- Also
Me.Print Check1.LBound
Me.Print Check1.UBound
End Sub

Jul 17 '05 #3
>
Three points. First, declare the ctrName argument as Object. Second, Dim
all of your variables. Right now, every variable in your Function is a
Variant. Variants execute slower than declared variables and they take
up more memory. Third, you don't need to pass the lower and higher
indexes of the control array because VB will already knows them. Inside
of the Function, you could set your For-Next loop like this (and
eliminate intLow and intHigh altogether).


Bingo! Thank you very much.

My variables were already declared, I was using them elsewhere in and was
only writing that function after writing 10 nearly identical prodedures.

I do need to pass the lower and higher indexes, unfortunatly I was running
out of room on the form thanks to having so many check boxes. Many arrays
for example have elements 0 to 13 as checkboxes, then another one at 99.

Very bad code I know, but I'll be able to clean it up after the deadline by
replacing the arrays with a user control.

Steve
Jul 17 '05 #4
> Option Explicit

Private Sub Command1_Click()
Call LS_SetCheckBoxes(Check1)
End Sub

Sub LS_SetCheckBoxes(Check1 As Object)
Dim C As CheckBox

For Each C In Check1
Select Case C.Value
Case vbChecked: C.Value = vbUnchecked
Case vbUnchecked: C.Value = vbChecked
End Select
Next
' --- Also
Me.Print Check1.LBound
Me.Print Check1.UBound
End Sub


Thank you very much.

I can't belive I had no idea about the Object variable.

Steve
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by ritchie | last post: by
14 posts views Thread by Abhi | last post: by
10 posts views Thread by Robert Dailey | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.