Vb为什么无法正确返回一个值

来源:百度知道 编辑:UC知道 时间:2024/06/04 18:41:54
Private Sub Command1_Click()
Dim A As Boolean
A = Chk(abc)
MsgBox A
End Sub

Private Function Chk(ctlName As String) As Boolean
Dim i As Integer
For i = 0 To Me.Controls.Count - 1
If Me.Controls.Item(i).Name = ctlName Then Chk = True: Exit Function
Next
End Function
为什么总是显示false, 但是窗体上我确实添加了一个名字为“abc”的按钮控件啊。

改成A = Chk("abc")
试试

Option Explicit '加上这一句,就能发现下面的错误!

Private Sub Command1_Click()
Dim A As Boolean
A = Chk("abc") '这里修改了!
MsgBox A
End Sub

Private Function Chk(ctlName As String) As Boolean
Dim i As Integer
For i = 0 To Me.Controls.Count - 1
If Me.Controls.Item(i).Name = ctlName Then Chk = True: Exit Function
Next
End Function

看看大小写对不对

因为Function中的ctlName As String是字符串型的数据,所以把前面的A = Chk(abc)改为A = Chk("abc")