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:
- How do I refer to/modify all the combo boxes in the entire database, or
- 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.