【问题标题】:Create ListBox MsgBox with Selected Items使用所选项目创建 ListBox MsgBox
【发布时间】:2014-04-18 15:21:36
【问题描述】:

我有一个列表框控件,它假设将所选项目的列表填充到 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

【问题讨论】:

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


    【解决方案1】:

    您正在循环访问 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
    

    【讨论】:

    • OK...当我这样做时,我仍然得到:ERROR: Compile error: Method or data member not found 并且它突出显示同一行中的 .List。跨度>
    • 我正在使用您的原始代码,因为我在这里没有访问权限。试试这个。我不是 100% 确定 .ListIndex = -1 将适用于多项选择,所以测试一下。
    • 非常感谢您的协助,抱歉回复晚了。将此信息发送到表格而不是 msgbox 有多难?
    • 绝对有可能。您将如何执行此操作取决于表单是否绑定到您要使用运输项目更新的表。加上所选项目是否将进入多行或多列。我认为对于另一个有更多解释的问题来说已经足够了。谢谢采纳!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多