标题: 一个根据名称类批量操作的函数 [打印本页] 作者: hunrybecky 时间: 2008-5-6 15:55 标题: 一个根据名称类批量操作的函数 Function hy_CtlBatProByName(frmForm As Form, strCtlLeftName As String, strOperName As String, Optional varOperValue As Variant)
'===============================================================================
'功能描述:根据控件最左边的字符批量执行操作
'参数说明:strOperName为操作参数,参数很多,具体参考下面的CASE语句
'使用示例:把所有以txtQ开始的控件(不一定是文本控件,只是一种命名策略而已)清空,hy_CtlBatProByName Me.Form, "txtQ", "Value", Null;又如移出所有标签的冒号;hy_CtlBatProByName Me.Form, "Label", "RemoveColon", true
'创建日期:2008-06-16 更新日期: 2008-06-16
'测试状态:OK
'===============================================================================
On Error Resume Next
Application.Echo False
Dim ctl As Control
For Each ctl In frmForm.Controls
If Left(ctl.Name, Len(strCtlLeftName)) Like strCtlLeftName Then
If strOperName = "Visible" Then '控件是否可见
ctl.Visible = varOperValue
ElseIf strOperName = "Enabled" Then '控件是否可用
ctl.Enabled = varOperValue
ElseIf strOperName = "Value" Then '批量设置控件值
If TypeOf ctl Is TextBox Or TypeOf ctl Is ListBox Or TypeOf ctl Is ComboBox Or TypeOf ctl Is CheckBox Then
ctl.Value = varOperValue
End If
ElseIf strOperName = "LimitToList" Then '限制到列表(组合框和列表框有效)
If TypeOf ctl Is ListBox Or TypeOf ctl Is ComboBox Then
ctl.LimitToList = varOperValue
End If
ElseIf strOperName = "Caption" Then '批量这是标题,如果操作值为空字符则清空所有
If TypeOf ctl Is Label Then
If Nz(varOperValue, "") = "" Then
ctl.Caption = ""
End If
End If
ElseIf strOperName = "RemoveColon" Then '移出或增加标签控件的冒号,此时不用指定操作值true(移出)或false(增加)
If ctl.Section = acDetail And (TypeOf ctl Is Label) Then
If Right(ctl.Caption, 1) = ":" Or Right(ctl.Caption, 1) = ":" Then
If varOperValue = True Then ctl.Caption = Left(ctl.Caption, Len(ctl.Caption) - 1)
Else
If varOperValue = False Then ctl.Caption = ctl.Caption & ":"
End If
End If
Else
MsgBox hy_LanguageMsgItem("1212"), , hy_LanguageMsgItem("1203")
Exit Function
End If
End If
Next ctl
Application.Echo True
End Function