这是我第一次在网站上发帖,虽然我找到了许多帮助我的答案。然而,尽管进行了广泛的研究,我仍无法找到这个具体问题的答案。
我能够为除了这一个(Field1)之外的所有必填字段开发代码。
区别在于Field1是一个带有复选框的下拉列表,可以选择多个复选框。
如何为以下代码调整此类型的字段?
ElseIf Len(Me.Category & "") = 0 Then
Cancel = True
response = MsgBox("You must enter a value in 'Category'.", vbInformation, "Mandatory Field")
Me!Category.SetFocus
Category
是我的其他字段之一,此代码工作正常,但是当我更改Field1的名称时,代码无效。
我希望我尽可能清楚,我真的很感谢社区的一些帮助
谢谢,
Abu Fulan
答案 0 :(得分:0)
我认为您要做的是将所有选定的项目放入一个数组中,然后确定数组的长度是否为> 0
虽然不完全相同,但我认为这个问题的接受答案(带有绿色复选标记的答案)可以略微修改,以便为您提供所需内容:
How do I return multi select listbox values into a sentence using Word VBA?
你可以删除数组部分并仍然使用代码:
Public Function GetSelectedItems(lBox As MSForms.ListBox) As String
Dim i As Integer
Dim selCount As Integer
selCount = -1
'## Iterate over each item in the ListBox control:
For i = 0 To lBox.ListCount - 1
'## Check to see if this item is selected:
If lBox.Selected(i) = True Then
'## If this item is selected, then add 1 to your selCount counter
selCount = selCount + 1
End If
Next
If selCount = -1 Then
'## If no items were selected, return a messagebox saying this is mandatory
msgbox "You MUST select something from the listbox."
Else:
'## Otherwise, don't worry about it
End If
End Function