无效使用空访问列表框

时间:2018-05-14 04:54:28

标签: listbox access-vba

我正在尝试计算从Multiselect列表框的列中选择的值的总和,并将其放入表单的文本框中。我已经尝试了下面显示的代码,但是我收到错误“无效使用空”

Private Sub SelectTreatment_Click()

Dim i As Integer
Dim sumduration As Integer
sumduration = 0

For i = 0 To Me.SelectTreatment.ListCount - 1

    If Me.SelectTreatment.ItemsSelected(i) Then
         sumduration = sumduration + Me.SelectTreatment.Column(4, i)
    End If

Next i


End Sub

有人能告诉我这段代码有什么问题或更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

我已经改变了你的代码。这应该可以解决问题。只需确保第5列(索引号4)中包含数值。

Private Sub SelectTreatment_Click()
Dim i As Integer
Dim sumduration As Integer
sumduration = 0
Dim frm As Form, ctl As Control, varItm As Variant

    Set frm = Forms!YourFormName
    Set ctl = frm!SelectTreatment
    For Each varItm In ctl.ItemsSelected
        sumduration = sumduration + ctl.Column(4, varItm)
    Next varItm
    Debug.Print sumduration
End Sub