Reference a field dynamically

  • Thread starter Thread starter Svein
  • Start date Start date
S

Svein

Hi, all!

I've tried to search the forum for an answer to this, but haven't been
successful.
I try to do the following:

To alter the label caption for a field programatically, so I can alter more
than one label using code.

Example:
I want labels showing 1:, 2:, 3:, 4: to alter their texts to:
1-1:, 1-2:, 2-1:, 2-2: when a certain condition occurs. In another occation,
the same fields should be called:
1-1:, 1-2:, 1-3:, 1-4: and so on.
To accomplish this, I'd like code to alter all labels at once for me, using
some kind of a loop or array.

Any ideas, anyone?

Thanx in advance & best regards

Svein
 
Hi,
you can use a following loop:

For Each ctl In me.Controls
if ctl.ControlType= acLabel then
select case ctl.Caption
case "1"
ctr.Caption="1-1"
...
....
....

perhaps you can also use labels' name in a loop:

for I=1 to 10
me("label" & I).caption=I
next I

something like this

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com
 
Thank you for an immediate response. However, as I was waiting for responses,
I came up with a solution myself. Just for the record, I'll copy it in here:
(The base is for logging measurement results when testing fiberoptical
splitters)

Dim measurefields As New Collection

Dim MyArray()

For Each ctl In Controls
If Left(ctl.Name, 1) = "M" Then 'All measurement fields starts with the
letter M and then a number, i.e M1, M2...

Mnumber = Mid(ctl.Name, 2)
Mnumber = CInt(Mnumber)

'Deactivate fields that will not be used, based on the splitter amount &
size
If Mnumber > stor2 Then
ctl.Enabled = False
Else: ctl.Enabled = True
End If
End If
If Left(ctl.Name, 1) = "M" Then
If ctl.Enabled = True Then measurefields.Add (ctl.Name)
End If
Next
w = målefelter.Count

ReDim MyArray(4)
j = 1
k = 1
i = 1

Do

Do
MyArray(j - 1) = k & " - " & i & ":"

i = i + 1

j = j + 1
Loop While i <= stor
i = 1
k = k + 1

Loop While k <= splittnevner 'splittnevner is the size of
each splitter

For y = 1 To w

tstsv = measurefields.Item(y)
Me.Controls(tstsv).Controls(0).Caption = MyArray(y - 1)

Next

If Me.Dirty = True Then
Me.Dirty = False

End If
 
Hi Svein,

Assume you have a form with a number of labels. These labels are named
Label0, Label1, Label2 etc. (the defaults in Access). Then add the following
code to the form:

Private Sub SetLabelCaptions()
Dim Ctrl As Control ' Any type of control
Dim Lbl As Label ' Specific for a label control
Dim LblNr As Integer
' Assume you have labels on your form
' that are named Label0, Label1, Label2 etc.
For Each Ctrl In Me.Controls
If Left(Ctrl.Name, 5) = "Label" Then
Set Lbl = Ctrl
LblNr = Mid(Ctrl.Name, 6, Len(Ctrl.Name) - 5) + 1
Lbl.Caption = LblNr ' Set new label value
End If
Next
End Sub

Private Sub form_load()
Call SetLabelCaptions
End Sub


The Load routine will call the SetLabelCaptions routine upon loading in this
example. The SetLabelCaptions routine loops through all the Form's controls
and process the ones that have a name that starts with "Label". Here I have
taken the sequence number of the control's name and put that as the caption
which is what you see on the form as a user. Here you add your specific
logic.

Good luck!
Vincent
 
Back
Top