首页 > 解决方案 > vba 将控制称为变量语法

问题描述

我正在使用 VBA 创建控件,并且无法通过将它们称为控件来设置字体。我给它们命名,如果我按名称引用它们,我可以修改字体。 我需要知道正确的语法才能完成这项工作。我想我已经尝试了所有组合,但没有一个成功。Me.(control variable name).Font

For CountRecords = 0 To rs.RecordCount - 1
    tempLeft = 6
    For countfields = 0 To rs.Fields.Count - 1
        tempname = rs.Fields.Item(countfields).Name & CountRecords
        frmtst.Controls.Add "forms.textbox.1", tempname
        Set ctl = Me.frmtst(tempname)
        Me.test.Font = 14 'set the font on a test textbox
        Me.Controls(tempname).Value.Font = 14 '****Trouble line ********
        ctl.Width = ((columnwidth(countfields) ^ 0.8) * 10) + 25
        ctl.Height = 24
        ctl.Left = tempLeft 'templeft + columnwidth(CountFields) + 18
        tempLeft = tempLeft + ctl.Width + 3
        ctl.Top = 20 * CountRecords + 3
        ctl = rs.Fields.Item(countfields).Value
        If rs.Fields.Item(countfields).Type = 6 Then 
            ctl = Format(ctl, "$#,##0.00")
        end if
    Next countfields
    rs.MoveNext
Next CountRecords

标签: excelvbacontrolsuserform

解决方案


在表单过程中使用 Me 可以替代frmtst上面的表单名称。Me.frmtst(tempname)双重参考也是如此。tempname您可以使用参考控件Me.tempname。设置字体Me.tempname.Font.Name = "Lucida Console"和设置字体大小Me.tempname.Font.Size = 10


推荐阅读