Ms access Access VBA:在窗体上迭代控件,仅显示某些控件类型具有的属性
我试图遍历表单上的控件,以显示支持ControlSource的控件的ControlSource属性 我发现识别具有ControlSource属性的控件相对容易。问题在于访问这些控件的属性。泛型控件对象没有ControlSource属性。如果我使用的是另一种语言,我只需将一个listbox控件强制转换为listbox对象,例如,以访问ControlSource属性。但我的理解是,在VBA中没有类型继承,所以我不能将一种类型的对象强制转换为另一种类型 那么,如何访问支持该属性的控件的ControlSource属性呢 以下是我目前的代码:Ms access Access VBA:在窗体上迭代控件,仅显示某些控件类型具有的属性,ms-access,vba,Ms Access,Vba,我试图遍历表单上的控件,以显示支持ControlSource的控件的ControlSource属性 我发现识别具有ControlSource属性的控件相对容易。问题在于访问这些控件的属性。泛型控件对象没有ControlSource属性。如果我使用的是另一种语言,我只需将一个listbox控件强制转换为listbox对象,例如,以访问ControlSource属性。但我的理解是,在VBA中没有类型继承,所以我不能将一种类型的对象强制转换为另一种类型 那么,如何访问支持该属性的控件的ControlS
Private Sub IterateControlsOnForm()
Dim frm As Form, formName As String
' The only controls that have a ControlSource property are either
' BoundObjectFrame controls or ListBox controls.
Dim ctrl As Control
Dim boundObjectFrame As boundObjectFrame, listBoxCtrl As listBox
Dim boundObjectFrameTypes() As String, listBoxTypes() As String
Dim ctrlType As String
formName = "MAINFORM"
' Useful way of populating a string array - use Split function.
' Array function only works with Variants.
boundObjectFrameTypes = _
Split("CheckBox,ComboBox,CustomControl,GroupLevel", ",")
listBoxTypes = Split("OptionButton,OptionGroup,TextBox,TextBox", ",")
' Assumes form is open.
Set frm = Forms(formName)
For Each ctrl In frm.Controls
' Ignore controls that do not have a ControlSource property.
ctrlType = TypeName(ctrl)
If IsStringInArray(ctrlType, boundObjectFrameTypes) Then
' **** FOLLOWING LINE FAILS ****
Set boundObjectFrame = ctrl
Debug.Print boundObjectFrame.Name & "(" & ctrlType & ") " & _
"ControlSource Property: " & boundObjectFrame.ControlSource
ElseIf IsStringInArray(ctrlType, listBoxTypes) Then
' **** FOLLOWING LINE FAILS ****
Set listBoxCtrl = frm.Controls(ctrl.Name)
Debug.Print listBoxCtrl.Name & "(" & ctrlType & ") " & _
"ControlSource Property: " & listBoxCtrl.ControlSource
End If
Next ctrl
End Sub
我尝试了两种方法将通用控件对象转换为具有ControlSource属性的更特定控件。请参阅两条注释“**以下行失败**”使用对象如何
Private Sub IterateControlsOnForm()
Dim frm As Form, formName As String
' The only controls that have a ControlSource property are either
' BoundObjectFrame controls or ListBox controls.
Dim ctrl As Control
Dim boundObjectFrame As Object, listBoxCtrl As Object
Dim boundObjectFrameTypes As String, listBoxTypes As String
Dim ctrlType As String
formName = "MAINFORM"
' Useful way of populating a string array - use Split function.
' Array function only works with Variants.
boundObjectFrameTypes = _
",CheckBox,ComboBox,CustomControl,GroupLevel"
listBoxTypes = ",OptionButton,OptionGroup,TextBox,TextBox"
' Assumes form is open.
Set frm = Forms(formName)
For Each ctrl In frm.Controls
' Ignore controls that do not have a ControlSource property.
ctrlType = TypeName(ctrl)
If InStr(boundObjectFrameTypes, "," & ctrlType) Then
Set boundObjectFrame = ctrl
Debug.Print boundObjectFrame.Name & "(" & ctrlType & ") " & _
"ControlSource Property: " & boundObjectFrame.ControlSource
ElseIf InStr(listBoxTypes, "," & ctrlType) Then
Set listBoxCtrl = frm.Controls(ctrl.Name)
Debug.Print listBoxCtrl.Name & "(" & ctrlType & ") " & _
"ControlSource Property: " & listBoxCtrl.ControlSource
End If
Next ctrl
End Sub
有了VBA,您还可以在下一步使用On Error Resume,尽管我同意通常最好避免错误
formName = "MAINFORM"
Set frm = Forms(formName)
For Each ctrl In frm.Controls
On Error Resume Next
Debug.Print ctrl.Name _
& " ControlSource Property: " & ctrl.ControlSource
If Err.Number = 438 Then
Err.Clear
End If
Next
好极了,工作得很好!我从未想过使用对象数据类型,因为我假设它没有ControlSource属性。对于遇到这个问题的其他人,我还发现了另一个解决方法:ctrl.Properties(“ControlSource”)也起作用,其中ctrl是一个控制对象,正如上面的代码所示。对象具有分配给它的任何属性。