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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference Control using variable

Status
Not open for further replies.

BrianRig

IS-IT--Management
Feb 20, 2002
3
0
0
US
I was wondering how to reference and update control on a form from a module without explicitly knowning the control name. For example, if I have a bunch of controls:
Forms!myForm!Control1
Forms!myForm!Control2
...
How could I loop through them to set their values using a for/next loop? This obviously does not work:
Dim i as Integer
For i = 1 to 50
("Forms!myForm!Control" & i) = "MyValue"
Next

I don't want to be forced to set each control:
Dim ctlMyControl as Control
Set ctlMyControl = Forms!myForm!Control1 ... etc.

I though of using the Eval() function...that returns the value but not the control reference. Any other ideas?

Thanks - Brian
 
When you referece a control by name you actually reference a variable, create by VB, which contains a reference to the control.
Controls "collection" (not a VB Collection) starts at 0.
Dim ctl as Control
For I = 0 To Me.Controls.Count - 1
Set ctl = Me.Controls(I)
Debug.print ctl.Name
next
OR
For Each ctl in Me.Controls
Debug.print ctl.Name
next
Compare Code (Text)
Generate Sort in VB or VBScript
 
The easiest way I can explain is to use control arrays on your form. You could then use the following:

Public Sub UpdateForm(frm As Form)
Dim lbl As Label
Dim cmd As CommandButton

For Each lbl In frm.lblSeat
lbl.Caption = ""
Next
For Each cmd In frm.cmdSeat
cmd.Visible = False
Next
End Sub

Hope This helps Anything is possible, the problem is I only have one lifetime.
 
I do not remember where I found this code so I can not give credit for it. this routine will load the current tab

SetTags Me, m_TabName


Sub SetTags(strFrmName As Form, strTabName As String)
' Comments : This will set the tags on the current form with the data that is loaded
' : Note not every control supports the tag so just ignore the error
' Parameters : strFrmName - strTabName
' Returns : - NA
' Created : 2/1/99
' Modified :
'
' --------------------------------------------------------
On Error GoTo SetTags_Err
Dim f As Form
Dim tbc As Control
Dim pge As Page
Dim ctl As Control
Dim ctlTbc As Control
Set f = strFrmName
Set ctlTbc = f!TabCtl 'tbc contains the value of the current tab index
' Return reference to currently selected page.
Set pge = ctlTbc.Pages(ctlTbc.Value)
' Enumerate controls on currently selected page.
For Each ctlTbc In pge.Controls
ctlTbc.Tag = ctlTbc.Value
Next ctlTbc
Set pge = Nothing

SetTags_Exit:
Exit Sub
SetTags_Err:
Resume Next
End Sub
 
Thanks for the tips...I think I've got it.
I belieive I can use the following syntax to get the results I need using the (Controls) array:

Dim i as Integer
For i = 1 to 50
Forms!MyForms.Controls("Control" & i) = "MyValue"
Next i
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top