Create single Private Sub Procedure to cover large number of dynamic combo boxes (with variable in name)

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Sorry my header is probably confusing, I was not sure how to summarise my query.

I have a form in access with a 49 combo boxes which I have named cmbFullName1, cmbFullName2, cmbFullName3, cmbFullName4 etc
I know I can refer to them like this in the code...
VBA Code:
        For i = 1 To PplCnt
            .Controls("cmbFullName" & i).Visible = True
            .Controls("cmbRIN" & i).Visible = True
            .Controls("txtRec" & i).Visible = True
            .Controls("lblFullName" & i).Visible = True
            .Controls("lblRIN" & i).Visible = True
But can I create a private sub procedure for the form to cover all of the combo boxes or do I have to create a change event for each one?
instead of this...
VBA Code:
Private Sub cmbFullName1_Change()
Private Sub cmbFullName2_Change()
Private Sub cmbFullName3_Change()
Private Sub cmbFullName4_Change()

I want to do something like this
VBA Code:
Private Sub cmbFullName & i_Change()

is this possible?
Dannielle
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Probably more than one way to do something.
You can create a function that does what you want, then in form design view, select all of the controls and for the event you need, enter the name of the function. However, if there is a need to know which control fired the event then that also has to be taken care of. One method is to code the function to accept the name of the control, but that would mean after selecting all and entering (e.g. in the click event on the property sheet) your function name e.g. myFunction() you'd then have to individually enter the control name, as in myFunction(cmbFullNameX) where X is 1, 2, etc. You could also consider having the function refer to Screen.ActiveControl instead, but not if the function is going to do something to a control that doesn't have the focus.

Another possibility would be to read the control Tag property in that function, but that would also require you to put something there for each control.
 
Upvote 0
Solution
Forgot to mention that the function has to be in a standard module.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top