I have an access form with like 5 dropdowns boxes, I need to check if the values of the dropdown boxes are unique before I approve the form. I can iterate over all the form elements but I want to be able to iterate only over dropdowns:
----------------------------------------------------------------------
|Id1 [dropdown values] Id2 [Dropdown values] Id3 [Dropdown Values] |
| |
| CollectionTask [Another dropdown] |
| []This is a checkbox |
| [Approve Button] [clear myForm btn] |
----------------------------------------------------------------------
'iterating over the form elements
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
'do something only for dropdown controls of form
Next
Instead of iterating over everything, I want to iterate over only the dropdown types. I feel like there should be a way to do it. I am working around this using the technique below but that's a lot of code.
'this code will do the trick but that means I will have to write a condition for every id
'which for me is lots of code (not being lazy but I think iterating over the form elements will be more efficient
If (id1.Value = id2.Value) Or (id1.Value = id3.Value) Or (id1.Value = id4.Value) then
Msgbox "make sure you are not duplicating ids"
End if
With the following code you can check all controls with the
ControlType
ofacComboBox
. Basically it just adds theComboBox
's value into anDictonary
checking each time to see if the value already exists in theDictonary
. You just need to determine what you want to happen if the value is already set in anotherComboBox
.This is freehand code but I am fairly certain everything is correct: