Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access vba-在特定窗体控件上循环_Ms Access_Vba_Ms Access 2003 - Fatal编程技术网

Ms access vba-在特定窗体控件上循环

Ms access vba-在特定窗体控件上循环,ms-access,vba,ms-access-2003,Ms Access,Vba,Ms Access 2003,我有一个带有5个下拉框的访问表单,在我批准表单之前,我需要检查下拉框的值是否唯一。我可以迭代所有表单元素,但我希望只能迭代下拉列表: ---------------------------------------------------------------------- |Id1 [dropdown values] Id2 [Dropdown values] Id3 [Dropdown Values] | |

我有一个带有5个下拉框的访问表单,在我批准表单之前,我需要检查下拉框的值是否唯一。我可以迭代所有表单元素,但我希望只能迭代下拉列表:

----------------------------------------------------------------------
|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
我不想遍历所有内容,而是只遍历下拉列表类型。我觉得应该有办法做到这一点。我正在使用下面的技术解决这个问题,但这需要很多代码

'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

使用以下代码,您可以使用
acComboBox
ControlType
检查所有控件。基本上,它只是将
组合框
的值添加到一个
命令
中,每次检查该值是否已经存在于
命令
中。如果该值已在另一个
组合框中设置,则只需确定要执行的操作

这是写意代码,但我相当确定所有内容都是正确的:

 Private Sub CheckCombos()
     Dim ctl As Control
     Dim dict As Variant

     Set dict = CreateObject("Scripting.Dictionary")


     For Each ctl In Me.Controls

         If ctl.ControlType = acComboBox Then 

             If Nz(ctl.Value,"") <> "" Then 

                 If dict.Exists(ctl.Value) Then 
                    Msgbox "Combobox: " & ctl.Name & " has the same value as: " & dict(ctl.Value)
                 Else 
                    dict.Add ctl.Value, ctl.Name
                 End If

             Else 
                 Msgbox "Empty Combobox" 
                 'Handle exit
             End If 

         End If
     Next
  End Sub
Private子检查组合()
Dim-ctl作为对照
Dim dict作为变体
Set dict=CreateObject(“Scripting.Dictionary”)
对于Me.Controls中的每个ctl
如果ctl.ControlType=acComboBox,则
如果Nz(控制值为“”),则
如果dict.Exists(ctl.Value),则
Msgbox“组合框:”&ctl.Name&“的值与:”&dict(ctl.value)的值相同
其他的
指令添加控制值,控制名称
如果结束
其他的
Msgbox“空组合框”
"把关",
如果结束
如果结束
下一个
端接头