如何用VBA代码连接控件及其标签



在MS Access中,当我通过可视化设计器创建新控件时,还有一个标签。标签已连接到控件。

现在,我用VBA代码创建控件和标签。

如何连接它们?

使用CreateControl函数创建标签时,应在Parent参数中传递TextBox的控件名称,如函数声明中所述:Function CreateControl(FormName As String, ControlType As AcControlType, [Section As AcSection = acDetail], [Parent], [ColumnName], [Left], [Top], [Width], [Height]) As Control

例如,用250对链接的TextBoxes和标签填充现有表单的函数:

Public Sub PopulateFormWithControls(fNAme As String)
'PopulateFormWithControls "msrForm4Table"
Dim f As Form
Dim ctrlFld As Control
Dim ctrlCap As Control
Dim i As Integer
Dim col As Byte
Dim Y As Long
DoCmd.OpenForm fNAme, acDesign
Set f = Forms(fNAme)
For i = 1 To 250
Set ctrlFld = CreateControl(f.name, acTextBox, , , , 4000 + 8000 * col, Y, 4000, 300)
ctrlFld.name = "fld" & Format(i)
Set ctrlCap = CreateControl(f.name, acLabel, , ctrlFld.name, , 0 + 8000 * col, Y, 4000, 300)
ctrlCap.name = "cap" & Format(i)
Y = Y + 300
If i Mod 100 = 0 Then
col = col + 1
Y = 0
End If
Next i
End Sub

调整控件的属性LabelName以保留标签的名称。

Access 2019 的新增功能

在旧版本中:标记标签、剪切标签、标记控件、将标签粘贴到控件中。

最新更新