Question

tldr: How do I refer to each combo box, on each form, in turn?

I discovered quite late in the game that the default for combo boxes is "LimitToList = False." This is very bad, because I have lots of combo boxes and no one should ever be adding or editing anything by typing over them. My users keep doing that, and I need them to stop.

I already know how to, e.g., change all my forms so that the split form orientation is "datasheet on the bottom." That is, I've already solved the problem of "open all the forms, change a setting on each one, rinse, repeat." What I have works beautifully:

1    Public Sub MakeSplitFormsAllBottom()
2    
3        Dim strForm As String, db As DAO.Database
4        Dim doc As DAO.Document
5        Set db = CurrentDb
6
7        For Each doc In db.Containers("Forms").Documents
8
9        strForm = doc.Name
10
11           DoCmd.OpenForm strForm, acDesign
12           Forms(strForm).SplitFormOrientation = 1
13
14       DoEvents
15       DoCmd.Close acForm, strForm, acSaveYes
16
17       Next doc
18 
19   End Sub

The trouble is that I can't quite figure out how to change a setting on all of the combo boxes on each of those forms.

I Googled around quite a bit and didn't find any good examples, so I took a (bunch of) shots in the dark which generally look like this:

1    Public Sub MakeAllCombosLimited()
2    
3        Dim strForm As String, db As DAO.Database, obj As AccessObject, strObj As String
4        Dim doc As DAO.Document
5        Set db = CurrentDb
6
7        For Each doc In db.Containers("Forms").Documents
8
9        strForm = doc.Name
10
11          DoCmd.OpenForm strForm, acDesign
12   
13           For Each obj In strForm
14              Forms(strForm).Controls(obj).LimitToList = True
15              DoEvents
16           Next obj
17    
18       DoEvents
19       DoCmd.Close acForm, strForm, acSaveYes
20
21       Next doc
22
23   End Sub

Nothing I have tried has worked. The one above gives me the error "For Each may only iterate over a collection object or an array." I also tried variations of:

7    For Each obj in db.Containers("Forms").Documents

To just refer to all the objects in the entire database, but that didn't work either - "Sub or Function not defined" is what I get, my guess that's because you need to say that the object belongs to a specific form.

I'm not sure what questions I should be asking, to be honest. It seems to me that the bottom line is I need to know at least one of the following:

  1. How do I refer to/modify all the combo boxes in the entire database, or
  2. How do I refer to/modify all the combo boxes in a given form?

But I know that I don't know the answer to either one. I feel like I'm making this harder than it needs to be.

Was it helpful?

Solution

Not sure if this will work in Access, but please try:

Dim ctrl as Control
For each ctrl in Form.Controls  'Revise to refer to your form object.
    If TypeOf(ctrl) Is MSForms.ComboBox
       'Do something with the ctrl, here:
       '
       '
    End If
Next

OTHER TIPS

Here is the set of code that actually did what I wanted it to do:

1    Public Sub MakeAllCombosLimited()
2    
3    Dim db As DAO.Database, strForm As String
4    Dim doc As DAO.Document
5    Set db = CurrentDb
6    
7    For Each doc In db.Containers("Forms").Documents
8        strForm = doc.Name
9        DoCmd.OpenForm strForm, acDesign
10   
11       For Each ctl In Forms(strForm).Controls
12            
13            If ctl.ControlType = acComboBox Then
14               ctl.LimitToList = True
15            End If
16       
17       Next ctl
18   
19       DoEvents
20       DoCmd.Close acForm, strForm, acSaveYes
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top