Ms access vba-在特定窗体控件上循环
我有一个带有5个下拉框的访问表单,在我批准表单之前,我需要检查下拉框的值是否唯一。我可以迭代所有表单元素,但我希望只能迭代下拉列表: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] | |
----------------------------------------------------------------------
|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“空组合框”
"把关",
如果结束
如果结束
下一个
端接头