How to obtain multi-value field (MVF) data from ComboBox control?

时间:2018-10-24 06:17:12

标签: vba ms-access

I have a (multi selection) Combo Box of supervisors, when choosing a supervisor from the Combo Box it populates a text box with the supervisors job ID. Now here is the tricky part, sometimes there are two supervisors that need to be selected, when I do that the textbox does not show anything.

So the question is, what is the correct VBA code to have the textbox display only the FIRST selected supervisor and ignore everything else after, and display that supervisors value in the textbox.

Here is the code which I have in vba at the moment. (P.S. the textbox also grabs some other data)

Private Sub SUPERVISOR_AfterUpdate()

Me.WORK_ID = Right(Me.SUPERVISOR.Column(2), 3) & Format(Me.RECEIVED_DATE, "yymmd")

End Sub

Here is a picture of what I refer to:

https://imgur.com/a/z982VjM

2 个答案:

答案 0 :(得分:2)

以下代码演示了如何从多值字段(MVF)组合框获取选定的值。我包括冗长的调试行,以演示如何推断出此信息,即使您不熟悉MVF或为什么它无法按预期工作。

我同意6月7日的评论,即仅从多个值之一创建ID是可疑的,但是设计决策超出了问题的范围。但是,至少该代码提供了足够的信息,用于遍历所有选定的值以生成所需的详细信息。

已更新代码更特定于问题(且没有冗长的调试):

  Dim supe1Data As String

  Dim va As Variant
  va = Me.SUPERVISOR.Value '* Multi-valued Field (MVF)

  Debug.Print "==============================="

  If IsNull(va) Then
    'Value is NULL if NO MVF list items are selected

    supe1Data = ""
  Else
    '* A Multi-valued Field (MVF) ComboBox will
    '*   return a variant array of variants.  The
    '*   array-element variants will be of a type
    '*   that is compatible with the underlying field value.

    '*** Question-specific code ***
    Dim iLower As Integer
    Dim iUpper As Integer

    iLower = LBound(va)
    iUpper = UBound(va)

    '* Get first selected supervisor code

    If iUpper = 0 Then
      '* Only ONE value selected, so
      '*    .Column(2) contains valid data
      '*    .Recordset property is NOTHING

      '* Contrary to online docs, for MVF...
      '*   Column(0) is bound data value
      '*   Column(i) is column i from 1 to .ColumnCount
      supe1Data = Me.MVF.Column(2)
    Else
      '* MULTIPLE value selected, so
      '*    .Column(2) is NULL
      '*    .Recordset property is valid and open

      Dim rs As Recordset2
      Set rs = Me.SUPERVISOR.Recordset.Clone

      '* Since data is already available, lookup can be 
      '*   more efficient than new query (i.e. using DLookup)
      rs.FindFirst "[LAST NAME] = '" & va(0) & "'"
      If Not rs.NoMatch Then
        '* Here, column indices start at 0
        supe1Data = rs.Fields(1).Value 'Second column
      Else
        supe1Data = "ERROR" 'Unexpected
      End If

      rs.Close
    End If
  End If

  Me.WORK_ID = Right(supe1Data, 3) & Format(Me.RECEIVED_DATE, "yymmdd")  

原始代码:

  Dim supe1 As String

  Dim va As Variant
  va = Me.SUPERVISOR.Value '* Multi-valued Field (MVF)

  If IsNull(va) Then
    'Value is Null if no MVF list items are selected
    Debug.Print "No Supervisor selected"

    supe1 = ""
  Else
    '* A Multi-valued Field (MVF) ComboBox will
    '*   return a variant array of variants.  The
    '*   array-element variants will be of a type
    '*   that is compatible with the underlying field value.

    'Discover details of the control value by inspecting
    '  the types of the returned values.
    Debug.Print "Field type: " & TypeName(va)

    Debug.Print "Variant array type: " & VarType(va)
    Debug.Print "   vbArray OR vbVariant = " & (VarType(va) Or vbArray)
    Debug.Print "   vbArray   = 8192"
    Debug.Print "   VbVariant = 12"

    'Inspect the first element
    Debug.Print "Element type: " & VarType(va(0))
    Debug.Print "   VbString = 8"

    Debug.Print "Array upper-bound: " & UBound(va)

    '*** Question-specific code ***
    Dim iLower As Integer
    Dim iUpper As Integer

    iLower = LBound(va)
    iUpper = UBound(va)

    supe1 = va(iLower)
    Debug.Print "First selected Supervisor: " & supe1
  End If

  Me.WORK_ID = Right(supe1, 3) & Format(Me.RECEIVED_DATE, "yymmdd")        

有关VarType()的更多信息,请参见Microsoft Docs

答案 1 :(得分:0)

好的,谢谢您,以上是当前代码,但仍然是一个问题。

所以问题是它仍然没有按照我的原始帖子所说的去做。只是要清楚。

示例:单击以选择主管,例如选择SUKDEO,然后选择BISSO。 SUKDEO是该工作的主要主管(我只需要填充他的帮派编号即可)。但是,使用此代码,当我选择SUKDEO然后选择BISSO时,Bissos帮派编号不会填充(返回到我的OP)优先选择的(或我应该说成主要的)超级用户。

更正:该问题仅在NEXT主管中发生。如果我选择其他所有主管,则该代码有效。请参阅图片。

希望如此,这是代码,这是图片。

does not work with next supervisor

only works with every other supervisor

Private Sub SUPERVISOR_AfterUpdate()
 Dim supe1Data As String

  Dim va As Variant
  va = Me.SUPERVISOR.Value '* Multi-valued Field (MVF)

  Debug.Print "==============================="

  If IsNull(va) Then
    'Value is NULL if NO MVF list items are selected

    supe1Data = ""
  Else
    '* A Multi-valued Field (MVF) ComboBox will
    '*   return a variant array of variants.  The
    '*   array-element variants will be of a type
    '*   that is compatible with the underlying field value.

    '*** Question-specific code ***
    Dim iLower As Integer
    Dim iUpper As Integer

    iLower = LBound(va)
    iUpper = UBound(va)

    '* Get first selected supervisor code

    If iUpper = 0 Then
      '* Only ONE value selected, so
      '*    .Column(2) contains valid data
      '*    .Recordset property is NOTHING

      '* Contrary to online docs, for MVF...
      '*   Column(0) is bound data value
      '*   Column(i) is column i from 1 to .ColumnCount
      supe1Data = Me.SUPERVISOR.Column(2)
    Else
      '* MULTIPLE value selected, so
      '*    .Column(2) is NULL
      '*    .Recordset property is valid and open

      Dim rs As Recordset2
      Set rs = Me.SUPERVISOR.Recordset.Clone

      '* Since data is already available, lookup can be
      '*   more efficient than new query (i.e. using DLookup)
      rs.FindFirst "[LAST NAME] = '" & va(0) & "'"
      If Not rs.NoMatch Then
        '* Here, column indices start at 0
        supe1Data = rs.Fields(1).Value 'Second column
      Else
        supe1Data = "ERROR" 'Unexpected
      End If

      rs.Close
    End If
  End If

  Me.WORK_ID = Right(supe1Data, 3) & Format(Me.RECEIVED_DATE, "yymmdd")
End Sub

我想这很清楚,我再次大声谢谢大家,看来我们快到了:)

-S