如何根据控件的名字判断某个窗体是否存在这个控件
大体思路这样
if   存在 Controls(controlName) then
 
controlNameEnabled = true
else …………
end if这个判断条件怎么写

解决方案 »

  1.   

    Private Sub Form_DblClick()
        Dim ctl As Control
        
        For Each ctl In Me.Controls
            If ctl.Name = "Text2" Then
                Debug.Print ctl.Text
            End If
        NextEnd Sub
      

  2.   

    Private Sub Command1_Click()
    Dim x As ControlFor Each x In Me.Controls
        Debug.Print x.Name
    Next x
    End SubPrivate Sub Command2_Click()
    Dim x As ControlFor Each x In Me.Controls
        If TypeOf x Is CommandButton Then Debug.Print x.Name
    Next x
    End Sub
      

  3.   


    可以不用遍历吗?
    因为这个是程序加载的时候我做的控件的 Enabled 属性值判断要是窗体控件将近255个,加载会不会速度很慢
      

  4.   


    Private Sub Command1_Click()
        Dim objP As Object
        For Each objP In Form1.Controls
            If objP.Name = "Command1" Then
                Debug.Print "属于该窗体"
            End If
        Next
    End Sub
      

  5.   

        On Error Resume Next
        Debug.Print TypeName(Me("text13"))
        If err.Number = 730 Then
            MsgBox "没有该控件"
        End If