创建函数以从列表框中获取值

时间:2016-05-25 13:43:07

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

Access 2010,Windows 7 有一个表单有几个带下拉列表的列表框。尝试创建一个函数,该函数将返回列表框中选定的行中的值。这是我的代码;

 Public Function listboxSel(listName As Control) As Integer
  Dim rowIndex As Integer
  Dim rowValue As String
  Dim rowIsSelected As Integer
  Dim result As String

  ' ListBox row index clicked
  rowIndex = Me.xxxxx.ListIndex

  ' Row value clicked
  rowValue = Me.xxxxx.Column(0)
  rowIsSelected = Me.xxxxx.Selected(rowIndex)
  listboxSel = rowValue
  End Function

首先,我不知道如何将列表框的名称传递给该函数。 其次,我不认为listName As Control是正确的。我一直在尝试在网上找到的一些代码无济于事。

1 个答案:

答案 0 :(得分:0)

为了获取列表框的选定项目,您可以在此处使用此代码。我有一个名为invoiceButton_Click的事件,它触发代码在列表框中找到所选项目并对其执行某些操作。我已经取出了我的代码,但留下了必要的内容。

'Moves selected item from items listbox to invoice listbox
Private Sub invoiceButton_Click()

Dim rowIndex As Integer, pos1 As Integer, i As Integer, rowValue As String, str As String, strSQL As String
Dim strToCut As String, tempStr As String, JobLinkCode As String, val As String

' ListBox row index clicked
rowIndex = Me.lbItems.ListIndex

'Checks if an item in the listbox is selected. If nothing is selected, end the program. If something is selected, Do Something.
'The .Column(0) is telling my code to use the first column of the listbox since they can contain multiple values.
If IsNull(Me.lbItems.Column(0)) Then
    End
Else

    'Do Something

End If

End Sub

此外,您不必将列表框传递给函数/子。您可以在该表单上的任何位置访问该控件,因此不必将其作为参数传递。只需使用我。[列表框名称]。[您要使用的功能]来访问它。

希望这有帮助!

相关问题