我正在尝试编写一个快速的VBA代码,用于检查2个组合框值是否在特定范围内匹配,如果是,则应返回错误消息。
我发现我的代码在将cboOption1.Column(0)
与cboOption1.Column(0)
进行比较时失败了。因此,我在额外的行And Not Me.Controls("cboOption" & i) = Not Me.Controls("cboOption" & k)
中添加了取消组合框对其自身进行的任何比较。但现在我收到错误消息,MS Access无法找到我在此行中的表达式中提到的字段组合框。知道出了什么问题吗?
Dim myArray As Variant
Dim x As Integer
myArray = Array(cboOption1.Column(0), cboOption2.Column(0), cboOption3.Column(0), cboOption4.Column(0))
For x = LBound(myArray) To UBound(myArray)
For i = 1 To 4
For j = 1 To 4
If Me.Controls("cboOption" & i).Column(0) = myArray(x) _
And Me.Controls("cboOption" & j).Column(0) = myArray(x) _
And Not Me.Controls("cboOption" & i) = Not Me.Controls("cboOption" & k) Then
MsgBox "You cannot select " & myArray(x) & " twice."
Exit Sub
End If
Next
Next
Next x
答案 0 :(得分:1)
在进行价值比较之前,如果i = j
进行基本检查怎么样?
Dim myArray As Variant
Dim x As Integer
myArray = Array(cboOption1.Column(0), cboOption2.Column(0), cboOption3.Column(0), cboOption4.Column(0))
For x = LBound(myArray) To UBound(myArray)
For i = 1 To 4
For j = 1 To 4
If i <> j Then
If Me.Controls("cboOption" & i).Column(0) = myArray(x) _
And Me.Controls("cboOption" & j).Column(0) = myArray(x) Then
MsgBox "You cannot select " & myArray(x) & " twice."
Exit Sub
End If
End If
Next
Next
Next x