Click to See Complete Forum and Search --> : Passing control arrays to a function / subroutine


paulc
March 19th, 2001, 03:54 AM
Hi all,

Curently, I am trying to use a subroutine in a module to create a control array, passing in the 0th member of the array as the parameter.

Here is the code in part which does this:

Sub CreateArray(ByRef frm As Form, ltblname As Control, lfldname() As Control, tflddata() As Control)

On Error GoTo CreateArrayError:

Dim varField As Variant
i = 0

' Setup dynamic array for controls that display data
For Each varField In gQuerySet.Fields
If i = 0 Then
' sets 0th label caption
lfldname(i).Caption = varField.name & ":"
nFieldType = varField.Type
' set size of 0th textbox
tflddata(i).width = GetFieldWidth(nFieldType)
If nFieldType = dbText Then
tflddata(i).MaxLength = varField.Size
tflddata(i).TabIndex = 0
End If
' bind textbox to datacontrol
tflddata(i).DataField = varField.name
i = i + 1
Else
' load ith label
Load lfldname(i)
lfldname(i).top = (i * gnCTLARRAYHEIGHT) + tflddata(0).top
lfldname(i).Visible = True
' load ith textbox
Load tflddata(i)
tflddata(i).top = lfldname(i).top
tflddata(i).Visible = True
' set ith label caption
lfldname(i).Caption = varField.name & ":"
nFieldType = varField.Type
' set textbox width
tflddata(i).width = GetFieldWidth(nFieldType)
If nFieldType = dbText Then
tflddata(i).MaxLength = varField.Size
tflddata(i).TabIndex = i
End If
' bind ith textbox to data control
tflddata(i).DataField = varField.name
i = i + 1
End If
Next

intFields = i

Exit Sub

CreateArrayError:

MsgBox Error(Err), vbOKOnly, "CreateArray"
Resume

End Sub


I am using the following code to call this function:

CreateArray frmEditor, lblTableName, lblfieldname, txtFieldData

but it don't work.

I have tried passing in the lblfieldname and txtfielddata controls as lblfieldname(i) and txtfielddata(i) and also setting the passed in control to lfldname(i) and tflddata().

Any ideas if this is even possible, or do I have to create the array first and then pass in the whole array to a function which can then so the sizing and placement etc.???

cheers

Paul

Cimperiali
March 19th, 2001, 04:21 AM
easiest way:
declare variant tipe variables
fill it with your controls array
(ie: dim Mylfldname As Variant, Mytflddata As Variant
Mylfldname = lfldname)
change your function to receive these variant
(ie: public sub myfunction(x as ..., Mylfldname as Variant,...)
and then, in your function
test for:
if typeof Mylfldname(0) is yourControlType then
or
if typeof Mylfldname(0) is Object
Hope this help



Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.