Results 1 to 7 of 7

Thread: Calculation using list box items

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    21

    Calculation using list box items

    Hi,

    I am benefited by this forum to a very great extent. Now for the past two days trying to do some calculations in the List Box items. I have two list boxes and the first list box contains the contents of a text file. Once the user clicks the item in List Box 1 same is added in List Box 2 and removed from List Box 1. This I am able to do without any difficulty. But I need to count the number of items added in List Box 2 and also the sum of a string in the List Box 2. I am pasting the code to this mail. Kindly advise me in this regard.
    Code:
    Private Sub lstDisplayrecord_Click()
    Dim Searchstring As String
    Dim Reasoncode As String
    Dim newstring As String
    Dim Linecnt As Integer
    Dim Totamt As String
    Searchstring = lstDisplayrecord.List(lstDisplayrecord.ListIndex)
    lblAccount = Mid(Searchstring, 10, 2)
    lblAccnum = Mid(Searchstring, 12, 15)
    lblTrandate = Format(Mid(Searchstring, 141, 8), "00/00/0000")
    lblName = Mid(Searchstring, 27, 40)
    lblAmount = Format(Mid(Searchstring, 116, 15) * 0.01, "#############.00")
    Reasoncode = InputBox("Enter Reason Code", "Reason Code", "01")
    'If Reasoncode = "01" Or "02" Or "03" Or "04" Or "05" Then
    Totamt = Totamt + lblAmount
    Searchstring = Left(Searchstring, Len(Searchstring) - 2) & Reasoncode
    lstrejectecs.AddItem Searchstring
    lblrejecsamt = Format(Totamt, "#############.00")
    Linecnt = Linecnt + 1
    lblrejecsnum = Linecnt
    lstDisplayrecord.RemoveItem lstDisplayrecord.ListIndex
    End Sub
    Last edited by Hack; Dec 20th, 2007 at 10:33 AM. Reason: Added Code Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Calculation using list box items

    To get a count of what is in List2
    Code:
    List2.ListCount
    To add up numbers in a listbox
    Code:
    Private Sub Command1_Click()
    Dim i As Long
    Dim lngTotal As Long
    
    For i = 0 To List1.ListCount - 1
        lngTotal = lngTotal + CLng(List1.List(i))
    Next
    
    MsgBox lngTotal
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    21

    Re: Calculation using list box items

    Hi Hack,
    While I have changed at necessary places (like List1 to whatever lst I have used). But it gives error with CLng. I am displaying a full line of a text file in the List 1 and I want to calculate the sum of a string (I am using Mid(Searchstring,116,15) the place of the string in that line of the text file. Kindly help me please.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Calculation using list box items

    try
    vb Code:
    1. clng(Mid(Searchstring,116,15))
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    21

    Re: Calculation using list box items

    Hi,

    Thanks it is working fine. Now I am facing with a different problem in the calculation. I am calculating the totals of two ListBox items. I have declared the variables and it works fine. But when the total number of records are equal in both the lists then it gives an error "Runtime Error 13" Type Mismatch.

    For example:
    Total 8 items are there in Listbox1
    I select 3 items and add it to Listbox2
    I get the total of amount column(string) of both the Listbox1 and Listbox2
    But as soon as I select the 4th item in Listbox1 then I get error Type Mismatch.

    I tried with a different file which was containing 96 records. Exactly after 47 records I got the same error.

    Kindly tell me where I am going wrong.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Calculation using list box items

    yes you need to stop at the end of the listbox with the least items, as listbox 2 probably has the least items use its count as the limit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Calculation using list box items

    And make sure you add some code to check if the ListCount = 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width