Adding a Control on the

For a normal control added at design time, it is easy to refer to the control by using its name:

Me.cbSave.Left = 100

But, for a control that is added at runtime, you have to use the Controls collection to set any properties for the control. Thus, it is important to set up a variable to hold the name of the control. Controls are added with the .Add method. The important parameter is the bstrProgld. This code name dictates whether the added control is a label, text box, command button, or something else.

The following code adds a new label to the form. PicCount is a counter variable used to ensure that each label has a new name. After the form is added, you should specify a position for the control by setting the Top and Left properties. You should also set a Height and Width for the control: LC = "LabelA" & PicCount

Me.Controls.Add bstrProgId:="forms.label.1", Name:=LC, Visible:=True

Me.Controls(LC).Top = 25

Me.Controls(LC).Left = 50

Me.Controls(LC).Height = 18

Me.Controls(LC).Width = 60

Me.Controls(LC).Caption = cell.value

0 0

Post a comment