Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control arrays in the controls collectin

Status
Not open for further replies.

j252ewv

Programmer
Jul 2, 2008
43
GB
Can any one tell/show me how to reference an elemetn of a control array from inside the controls collection here is what i want to to but it does not work as it requires the index to work out which coltrol to access

For intCounter = 0 To Me.Controls.Count
Set c = Me.Controls(intCounter)
If TypeName(c) = "TextBox" Then
frmRecords.Controls(c.Name).Text = c.Text
ElseIf TypeName(c) = "OptionButton" Then
frmRecords.Controls(c.Name).Value = c.Value
End If
Next

Help!
 
You are looping one time too many try;
For intCounter = 0 To Me.Controls.Count - 1

It is generally preferrable to loop through collections using For/ Each as in;

Dim cntrl as Control
For Each cntrl in Controls()
'Do stuff
Next

You should be able to determine if a Control is a member of a Control Array by attempting to return its Index Property; a trappable error should result on a Control which is not in a Control Array.




 
can you give me anexaple of how you would do this using a trapable error and how you would cath the error
 
On a Form with a Command button placed at top right and a Control Array of any Control type you like below it.

Private Sub Command1_Click()

Dim cntrl As Control
Dim i As Integer

AutoRedraw = True

For Each cntrl In Controls()
On Error Resume Next
i = cntrl.Index
Select Case Err.Number
Case 0
Print cntrl.Name & " is in a Control Array its index = " & cntrl.Index
Case 343 'Object not an array
Print cntrl.Name & " is not in a Control Array"
Case Else
'should not happen
Print "Unanticipated Error " & Err.Number & " " & Err.Description
End Select
Err.Clear
Next

End Sub
 
That is spot one , as soon as i saw your code i don't know why i did not think of that.

Many thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top