下記のようなフォームがあり、各ボタンを押すと、該当する地域のボタンも押せなくなります。
<例>
全国ボタン押→解除以外が押せなくなり、解除は押せるようになる
関西ボタン押→関西・大阪が押せなくなり、それ以外は押せるようになる
最初は以下のように書いていたのですが、繰り返しになるので、
AccessVBA
Private Sub 全国ボタン_Click() If 全国ボタン.Enabled Then 全国ボタン.Enabled = False If 関東ボタン.Enabled Then 関東ボタン.Enabled = False If 東京ボタン.Enabled Then 東京ボタン.Enabled = False If 神奈川ボタン.Enabled Then 神奈川ボタン.Enabled = False If 関西ボタン.Enabled Then 関西ボタン.Enabled = False If 大阪ボタン.Enabled Then 大阪ボタン.Enabled = False If Not 解除ボタン.Enabled Then 解除ボタン.Enabled = True End Sub
以下のように変更しました。
押下可・不可のリスト(HenkoBottons)と、可・不可への変更情報(onoff)を、関数BottonONOFFで受け取り、処理
AccessVBA
Private Sub 関西ボタン_Click() Call BottonONOFF("全国,関東,東京,神奈川,解除", True) Call BottonONOFF("関西,大阪", False) End Sub Function BottonONOFF(HenkoBottons As Variant, onoff As Boolean) Dim EachControl As Control Dim HenkoBotton As Variant HenkoBottons = Split(HenkoBottons, ",") For Each EachControl In Me.Controls If TypeName(EachControl) <> "CommandButton" Then GoTo Continue For Each HenkoBotton In HenkoBottons If InStr(EachControl.Name, HenkoBotton) Then If Not EachControl.Enabled = onoff Then EachControl.Enabled = onoff Next Continue: Next End Function
まだ回答がついていません
会員登録して回答してみよう