简体   繁体   English

从 access 中的多值组合框获取值 vba

[英]Get values from a multi-value combo box in access vba

I have a form that I'm using to gather information that will be inserted into a database table in Access 2016. My form has a multi-value combo box (called cmbContacts) that pulls values from a Contacts table (ID and Name).我有一个表单,用于收集将插入到 Access 2016 中的数据库表中的信息。我的表单有一个多值组合框(称为 cmbContacts),它从联系人表(ID 和名称)中提取值。 A user is able to select 0 or more contacts from the combo box.用户可以从组合框中拨打 select 0 个或多个联系人。 When a user clicks the submit button, the button_click event triggers VBA code to format the information and insert it into the table.当用户单击提交按钮时,button_click 事件触发 VBA 代码来格式化信息并将其插入到表中。

What I need to do is get the IDs of each selected contact from the combo box.我需要做的是从组合框中获取每个选定联系人的 ID。

Here's what I have so far:这是我到目前为止所拥有的:

If Me.cmbContacts.ItemsSelected.Count > 0 Then 'Only run code if there are contacts selected'
    'Select the most recently added entry (added right before this if statement)'
    Dim rs As DAO.Recordset, Max As Integer
    SQL = "SELECT MAX(ID) FROM Breweries"
    Set rs = CurrentDb.OpenRecordset(SQL)
    Max = rs.Fields(0)
    'Insert each selected Contact ID into Breweries.[Contact Names].Value'
    Dim itm As Variant
    For Each itm In Me.cmbContacts.ItemsSelected
        SQL = "INSERT INTO Breweries ([Contact Names].[Value]) VALUES (" + itm + ") WHERE ID=" + Max
        CurrentDb.Execute (SQL)
    Next itm
End If

Some issues:一些问题:

  • Me.cmbContacts.ItemsSelected.Count always equals 0, regardless of how many items are selected in the Combo Box Me.cmbContacts.ItemsSelected.Count 始终等于 0,无论在组合框中选择了多少项目
  • Because cmbContacts.ItemsSelected doesn't contain the selected items, the for loop doesn't run.因为 cmbContacts.ItemsSelected 不包含所选项目,所以 for 循环不会运行。 I'm not sure why it's not registering the selected items我不确定为什么它没有注册所选项目

If a combobox has more than 1 item selected/populated, then you can use Ubound() to find out how many.如果组合框选择/填充了 1 个以上的项目,那么您可以使用 Ubound() 找出有多少。

IF UBound(Me.cmbContacts.OldValue) > 0
    For i = LBound(Me.cmbContacts.OldValue) To UBound(Me.cmbContacts.OldValue)
       SQL = "INSERT INTO Breweries ([Contact Names].[Value]) VALUES (" + Me.cmbContacts.OldValue(i) + ") WHERE ID=" + Max
        CurrentDb.Execute (SQL)
    Next i
END IF

Bear in mind this will error with a Type Mismatch if only one, or no values are selected and you will have to handle this somehow.请记住,如果仅选择一个或未选择任何值,则会出现类型不匹配的错误,并且您必须以某种方式处理此问题。

Hope this helps..希望这有帮助..

After a lot of searching on MVF's and VBA all I found was working with parent/child recordsource.在对 MVF 和 VBA 进行大量搜索后,我发现所有这些都在使用父/子记录源。 In my case the user has selected the values but they are not saved to the table yet.在我的例子中,用户已经选择了值,但它们还没有保存到表中。 I just needed a way to loop through the control and get all the values selected.我只需要一种方法来遍历控件并获取所有选定的值。 Couldn't find this anywhere - in case it's helpful to someone.无法在任何地方找到它 - 如果它对某人有帮助。

 For Each Val in me.MultiValueComboBoxName.Value
    'Do Action
 Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM