访问子引用中传递的框架的控件

我试图将用户窗体的许多MSForms.Frames传递给应该做的东西与他们包含的文本框的子:

initFrame(frame1) 

但是,一旦通过,我不能访问该框架的.Controls属性了(在debugging时添加了一个手表,剩下的只剩下Items)。

我已经尝试了许多不同的子声明,但它们要么不编译,要么丢失属性…

 Private Sub initFrame(ByRef currFrame As MSForms.Frame) Private Sub initFrame(ByRef currFrame As MSForms.Object) Private Sub initFrame(ByRef currFrame As Frame) Private Sub initFrame(ByRef currFrame As Object) 

在任何情况下,我都会遇到运行时错误

 For Each ctl In currFrame.Controls 

我必须做一些特殊的事情来访问控制? 像铸造?

这里是整个代码(忽略.Name分配):

 Private Sub initTabs() initFrame (frPrDetails) initFrame (frPcDetails) initFrame (frScDetails) End Sub Private Sub initFrame(ByRef currFrame As Frame) Dim ctl As Control With currFrame For Each ctl In .Controls If TypeName(ctl) = "TextBox" Then If ctl.TabIndex <> 57 Then Select Case (ctl.TabIndex) Mod 7 Case 1 'ctl.Name = "tb" & ctl.tag & "MP" Case 2 'ctl.Name = "tb" & ctl.tag & "HW" Case 3 'ctl.Name = "tb" & ctl.tag & "SW" Case 4 'ctl.Name = "tb" & ctl.tag & "IC" Case 5 'ctl.Name = "tb" & ctl.tag & "ES" Case 6 'ctl.Name = "tb" & ctl.tag & "CONT" Case 0 'ctl.Name = "tb" & ctl.tag & "ST" End Select Else 'ctl.Name = "tbPrTotal" End If ctl.Text = ctl.Name End If Next ctl End With End Sub 

原因很简单,但非常微妙。

首先,默认情况下,参数是通过引用传递的,所以:

 Private Sub initFrame(currFrame As MSForms.Frame) ' ByRef is optional 

但是,如果参数是一个expression式,那么将创build一个新的对象/variables。
所以相信与否, (frame1)frame1是不一样的。

回到问题中,当您调用函数时,请使用:

 Call initFrame(frPrDetails) ' note the lack of space before the opening bracket 

或干脆

 initFrame frPrDetails 

但不是相当于Call initFrame((frPrDetails))

Interesting Posts