Home > database >  How to change visibility of a combo box based on another combo box value in VBA
How to change visibility of a combo box based on another combo box value in VBA

Time:11-12

I am using Microsoft Access at the moment.

I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:

Private Sub cmb_BoxValue_AfterUpdate()
 Dim Size1 As Integer
 Size1 = Me.cmb_BoxValue.Value

 Select Case Me.cmb_BoxValue
  Case 1
  Me.cmb_boxBox1 = True
  etc...

But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you

CodePudding user response:

Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop

For x = 1 to 20
    Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next
  • Related