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

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/23 07:45