Share via

Bring total of listbox column by vba

Anonymous
2018-01-21T08:16:51+00:00

I am trying to bring the total of listbox column by vba but not getting the correct result :

My complete vba code is as below :

Private Sub Label27_Click()

If Me.StartDate = "" Or IsNull(Me.StartDate) Or Me.EndDate = "" Or IsNull(Me.EndDate) Then

MsgBox "Date fields are empty", vbCritical

Me.StartDate.SetFocus

Else

Me.SearchBox.Requery

Me.SearchBox.SetFocus

If Me.SearchBox.ListCount = 0 Then

Me.Label125.Caption = "Total Records Found : " & Me.SearchBox.ListCount

Else

Dim SearchTotal As Double

        Dim i As Integer

        For i = 0 To SearchBox.ListCount - 1

            SearchTotal = SearchTotal + Val(SearchBox.Column(6, i))

            ' seventh column is my value field

        Next

Me.Label125.Caption = "Total Records Found : " & Me.SearchBox.ListCount - 1 & "   :  Amount " & SearchTotal

End If

Me.SearchBox.Value = Me.SearchBox.ItemData(Abs(Me.SearchBox.ColumnHeads))

Me.SearchBox.SetFocus

End If

End Sub

There is no error coming. but not coming the correct result - total record is listbox after query is 3 and amount should be 4380

while it showing the count correct but the total amount 4

could not understand what is the mistake in the vba code.

Note : My listbox record source is based on sql code, so cannot use Dsum. so I am trying to avoid making query just for this and obtain from vba if possible

Please advice.

Regards

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2018-01-21T10:40:24+00:00

    Check out what values actually are returned:

    For i = 0 To SearchBox.ListCount - 1

        Debug.Print i, Val(SearchBox.Column(6, i))

        SearchTotal = SearchTotal + Val(SearchBox.Column(6, i))

    Next

    Also, as you seem to use column heads, try:

    For i = 1 To SearchBox.ListCount

        Debug.Print i, Val(SearchBox.Column(6, i))

        SearchTotal = SearchTotal + Val(SearchBox.Column(6, i))

    Next

    1 person found this answer helpful.
    0 comments No comments

Answer accepted by question author

  1. Anonymous
    2018-01-21T12:17:41+00:00

    That was what I expected. The amount is formatted with a thousand separator which is a show-stopper for Val().

    Try this, still declaring SearchAmount as Currency:

    SearchTotal = SearchTotal + CCur(SearchBox.Column(6, i)))

    0 comments No comments

Answer accepted by question author

  1. Anonymous
    2018-01-21T09:44:50+00:00

    ciao Irshad,

    assiming orders Northwind database example provided by Microsoft :

    after creating a form like this, openinig it you will see all datas:

    you achieve your task clicking on update command button whose event click code is :

    Option Compare Database

    Option Explicit

    Private Sub cmdUpdate_Click()

    With Me

        .txtMessage = Null 

        If Len(.fromDate & vbNullString) = 0 Or Len(.toDate & vbNullString) = 0 Then

            VBA.MsgBox prompt:="date criteria is wrong..!", _

                       Buttons:=vbCritical, _

                       Title:="Warning!!"

            Exit Sub

        End If

        .lstOrders.Requery

        If .lstOrders.ListCount > 0 Then

            .txtMessage = "totRows:" & .lstOrders.ListCount & ", totValue:" & .lstOrders.Column(4, 0)

        End If

    End With

    End Sub

    and 

    sql code under listbox control is :

    PARAMETERS [forms]![maschera11]![fromDate] DateTime, [forms]![maschera11]![toDate] DateTime;

    SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, Orders.Freight,

                (select sum(Freight) from orders where orderdate>=forms!maschera11!fromDate and orderdate<forms!maschera11!toDate+1 Or [forms]![maschera11]![fromDate] Is Null Or [forms]![maschera11]![toDate] Is Null) AS totFreight

    FROM

           Orders

    WHERE

         Orders.OrderDate>=[forms]![maschera11]![fromDate] And (Orders.OrderDate)<[forms]![maschera11]![toDate]+1

     OR

       [forms]![maschera11]![fromDate] Is Null OR [forms]![maschera11]![toDate] Is Null;

    or like this :

    PARAMETERS [forms]![maschera11]![fromDate] DateTime, [forms]![maschera11]![toDate] DateTime;

    SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, Orders.Freight,

                  (select sum(Freight) from orders where orderdate>=forms!maschera11!fromDate and orderdate<forms!maschera11!toDate+1 ) AS totFreight

    FROM

            Orders

    WHERE

           Orders.OrderDate>=[forms]![maschera11]![fromDate] And (Orders.OrderDate)<[forms]![maschera11]![toDate]+1;

    maybe the latter suits better your request since you are validating date values range.

    in my opionion is an overKill looping through listobox item to sum each one to figure out the total value.

    it's convenient add a calculated field on listbox sql predicate and show its content in txtMessage textbox control simply concatenating its value with listCount property.

    HTH.

    Ciao, Sandro.

    0 comments No comments

14 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-01-21T10:24:56+00:00

    Sir,

    did the changes as below, still the incorrect result :

    Private Sub Label27_Click()

    If Me.StartDate = "" Or IsNull(Me.StartDate) Or Me.EndDate = "" Or IsNull(Me.EndDate) Then

    MsgBox "Date fields are empty", vbCritical

    Me.StartDate.SetFocus

    Else

    Me.SearchBox.Requery

    Me.SearchBox.SetFocus

    If Me.SearchBox.ListCount = 0 Then

    Me.Label125.Caption = "Total Records Found : " & Me.SearchBox.ListCount

    Else

    Dim SearchTotal As Currency

            Dim i As Integer

            For i = 0 To SearchBox.ListCount - 1

                SearchTotal = SearchTotal + Val(SearchBox.Column(5, i))

            Next

    Me.Label125.Caption = "Total Records Found : " & Me.SearchBox.ListCount - 1 & "   :  Amount " & SearchTotal

    End If

    Me.SearchBox.Value = Me.SearchBox.ItemData(Abs(Me.SearchBox.ColumnHeads))

    Me.SearchBox.SetFocus

    End If

    End Sub

    Please also note the sql code used for List box :

    SELECT WcExpoQA.FormNo, WcExpoQA.ClaimedDate, WcExpoQA.Customer, WcExpoQA.Product, WcExpoQA.Model, WcExpoQA.SerNo, WcExpoQA.ClaimAmt, WcExpoQA.JobDetail FROM WcExpoQA;

    Please advice.

    regards

    0 comments No comments
  2. Anonymous
    2018-01-21T09:03:45+00:00

    It sounds like you are retrieving the wrong column - these are zero-based. So try:

    Val(SearchBox.Column(5, i))

    And always use data type Currency when handling amounts:

    Dim SearchTotal As Currency

    0 comments No comments