原代码如下:
Private Sub ComSave_Click() '保存按钮 If ComSave.Caption = "编辑(自动保存)" Then Me.Text160.Locked = False Me.Text138.Locked = False Me.FM入库单.Form.数量.Locked = False Me.FM入库单.Form.单价.Locked = False Me.FM入库单.Form.备注.Locked = False ComSave.Caption = "退出编辑" Else Me.Text160.Locked = True Me.Text138.Locked = True Me.FM入库单.Form.数量.Locked = True Me.FM入库单.Form.单价.Locked = True Me.FM入库单.Form.备注.Locked = True ComSave.Caption = "编辑(自动保存)" End If End Sub
使用数组后的代码如下:
Private Sub ComSave_Click() Dim lockedControls As Variant Dim control As Variant ' 根据当前按钮的标题确定要锁定/解锁的控件数组 lockedControls = Array(Me.Text160, Me.Text138, Me.FM入库单.Form.数量, Me.FM入库单.Form.单价, Me.FM入库单.Form.备注) '定义数组 If ComSave.Caption = "编辑(自动保存)" Then ComSave.Caption = "退出编辑" Else ComSave.Caption = "编辑(自动保存)" End If ' 锁定/解锁控件 For Each control In lockedControls control.Locked = Not control.Locked Next control End Sub
用循环和数组可以避免多次重复设置控件的Locked属性,并使代码更加简洁。这种方法适用于任意数量的控件,并可以方便地进行拓展和修改。
标签:Me,控件,Locked,Form,批量,ComSave,ACCESS,FM,入库 From: https://www.cnblogs.com/yoooos/p/17617871.html