|
-
Dec 20th, 2007, 10:22 AM
#1
Thread Starter
Junior Member
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
-
Dec 20th, 2007, 10:36 AM
#2
Re: Calculation using list box items
To get a count of what is in List2To 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
-
Dec 21st, 2007, 01:12 AM
#3
Thread Starter
Junior Member
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.
-
Dec 21st, 2007, 07:57 PM
#4
Re: Calculation using list box items
try
vb Code:
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
-
Dec 21st, 2007, 08:44 PM
#5
Thread Starter
Junior Member
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.
-
Dec 21st, 2007, 09:35 PM
#6
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
-
Dec 22nd, 2007, 06:01 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|