简体   繁体   English

vba 将控件称为变量语法

[英]vba refer to control as variable syntax

I am creating controls with VBA and cannot set the font by referring to them as a control .我正在使用 VBA 创建控件并且无法通过将它们称为控件来设置字体 I name them and can modify the font if I refer to them by name Me.(control variable name).Font .我命名它们,如果我通过名称Me.(control variable name).Font引用它们,则可以修改字体。
I need to know the proper Syntax to make that work.我需要知道正确的语法才能完成这项工作。 I think I've tried every combination but none has been successful.我想我已经尝试了所有组合,但没有一个成功。

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

Using Me in a form procedure is a replacement for the form name, which seems to be frmtst above.在表单过程中使用 Me 是表单名称的替代,它似乎是上面的frmtst So Me.frmtst(tempname) is a double refer.所以Me.frmtst(tempname)是双重引用。 You can refer to the control tempname with Me.tempname .你可以参考控制tempnameMe.tempname Set the font with Me.tempname.Font.Name = "Lucida Console" and set font size with Me.tempname.Font.Size = 10使用Me.tempname.Font.Name = "Lucida Console"设置字体并使用Me.tempname.Font.Size = 10设置字体大小

How to reference controls properly如何正确引用控件

You can reference controls您可以参考控件

  • 1a) directly by Name and using IntelliSense (eg Me.Test ), 1a) 直接按名称并使用 IntelliSense(例如Me.Test ),
  • 1b) indirectly via the Controls collection or 1b) 间接通过Controls集合或
  • 2) implicitly via setting an object directly or indirectly (eg Set ctl = Me.Controls(tempname) ) 2)通过直接或间接设置对象隐式(例如Set ctl = Me.Controls(tempname)

Note that the particle Me always refers to the current UserForm instance (not to its name) and can/should be used within the userform code module.请注意,粒子Me始终指的是当前的 UserForm 实例(而不是其名称),并且可以/应该在 userform 代码模块中使用。 For instance you can refer to Me.Controls or a given item within the controls' collection, eg Me.Controls(tempname) .例如,您可以引用Me.Controls或控件集合中的给定项,例如Me.Controls(tempname) - It's bad use , however to refer to the default instance of a UserForm (eg frmtst ) from that form's code behind. - 这是不好的用法,但是从表单后面的代码中frmtst用户表单的默认实例(例如frmtst )。 Furthermore it's impossible to refer to both within the same statement like Me.frmtst(tempname) .此外,不可能在Me.frmtst(tempname)之类的同一语句中同时引用两者。

Suggested reading for a deeper understanding : UserForm1.Show?建议阅读以加深理解UserForm1.Show?

1a) Missing .Size property in test assignment to .Font 1a)中缺少.Size在测试分配给属性.Font

  ' Direct referencing a control of the current Userform instance - missing .Size property
    Me.Test.Font.Size = 14              ' instead of: Me.test.Font = 14 

1b) Bad insertion of .Value property before failing .Font property 1b) 在 .Font 属性失败之前错误插入.Value属性

  ' Indirect referencing a control of the current Userform instance - bad .Value prop, .Font prop without .Size 
    Me.Controls(tempname).Font.Size = 14        ' instead of: Me.Controls(tempname).Value.Font = 14 

2) Object reference 2) 对象引用

If, however you prefer to set an object to the memory, the code line as shown in case [1b] is redundant and you should decide to stick to the chosen method.但是,如果您更喜欢将对象设置到内存中,则案例 [1b] 中所示的代码行是多余的,您应该决定坚持选择的方法。

Dim ctl As MsForms.TextBox              ' declare MSForms object (e.g. TextBox, or Object)
Set ctl = Me.Controls(tempname)         ' instead of: Set ctl = Me.frmtst(tempname)
ctl.Font.Size = 14                  ' instead of: .Font = 14 

Further remarks补充说明

Always use Option Explicit to check the correct and complete declaration of all variables (would have been fine to include some in your code).始终使用Option Explicit检查所有变量的正确和完整声明(在代码中包含一些会很好)。

BTW did you actually calculate the control's width via an exponent, ie ^ 0.8) * 10) + 25 ?顺便说一句,您实际上是否通过指数计算控件的宽度,即^ 0.8) * 10) + 25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM