使用所选项创建ListBox MsgBox

时间:2014-04-18 15:21:37

标签: ms-access compiler-errors listbox access-vba listboxitem

我有一个列表框控件,可以填充选中的项目列表到MsgBox,但是当我运行它时,我一直收到错误代码。

错误:编译错误:找不到方法或数据成员

我做错了什么?

Private Sub ctrSend_Click()
    Dim msg As String
    Dim i As Integer
    Dim lstMsg As ListBox

    If lstShipping.ListIndex = -1 Then
        msg = "Nothing"
    Else
        msg = ""
        For i = 0 To lstShipping.ListCount - 1
            If lstShipping.Selected(i) Then _
                msg = msg & lstMsg.List(i) & vbCrLf
        Next i
    End If

    MsgBox "You selected: " & vbCrLf & msg, vbOKOnly, "Selected BIN"
    Unload Me
End Sub

1 个答案:

答案 0 :(得分:0)

您正在循环使用lstShipping,但正在查看来自lstMsg的项目。这真的是你想要做的吗?如果没有,请将lstMsg更改为lstShipping,如下所示:

Private Sub ctrSend_Click()
  Dim msg As String
  Dim i As Integer
  Dim oItem as Variant
 ' Dim lstMsg As ListBox

 If lstShipping.ListIndex = -1 Then
   msg = "Nothing"
 Else
   msg = ""
   For Each oItem in lstShipping.ItemsSelected         
      msg = msg & lstShipping.ItemData(oItem) & vbCrLf   ' <---  lstShipping!         
   Next
 End If

 MsgBox "You selected: " & vbCrLf & msg, vbOKOnly, "Selected BIN"
 DoCmd.Close
End Sub