多选组合框 - 必填字段

时间:2015-12-14 16:36:26

标签: vba ms-access access-vba ms-access-2010 ms-office

这是我第一次在网站上发帖,虽然我找到了许多帮助我的答案。然而,尽管进行了广泛的研究,我仍无法找到这个具体问题的答案。

  • 我有一个包含多个字段的表单,其中一些是强制性的,另一些则不是
  • 我能够为除了这一个(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

1 个答案:

答案 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
相关问题