AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Syed Rizwan Muhammad Rizvi
Expertise
I can answers questions regarding web based and desktop based programming in VB.Net. Which can include SOAP, XML, Custom Controls, COM Interoperability etc.

Experience
Have been working in this specific area for last 2 years previously I was a VB 6 Developer with experties in other languages as well. Total 10 years of programming experience.

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Logical Error in Accounts Distribution

Topic: VB.NET



Expert: Syed Rizwan Muhammad Rizvi
Date: 1/13/2007
Subject: Logical Error in Accounts Distribution

Question
Hi: There are two list box.In the listbox name lstOpenAccounts there are three hundred values. In another listbox name lstseldist there are two values but it could be three or four. Assume from the first listbox name lstOpenAccounts I select 150 Accounts and in the second list box name lstseldist there are two name for accounts distribution. If its a 150 values in lstOpenAccounts then 75 each will be distribute to each name which are in lstseldist but the program gives 76 to one and 74 to another. Its total is 150 but the distribution is wrong. The Program goes to Case Else in function distributeByQuantity but it not distribute values correctly. Dont know what logic i use so that the accounts will be distributed correctly. NEEDS HELP


Private Sub cmdAssignAccounts_Click()
Dim multiUserDistCount As Integer

If lstDist.SelCount = 1 Then
   multiUserDistCount = 0
'Multiple users acquiring accounts
ElseIf lstDist.SelCount > 1 Then
   multiUserDistCount = lstOpenAccounts.SelCount / lstDist.SelCount
ElseIf lstDist.SelCount = 0 Then
   MsgBox "You must select the users to receive accounts. "
   Exit Sub
End If

Screen.MousePointer = vbHourglass
Set AcctsToDist = Nothing
If optEvenDollarDist.Value = True Then
   distributeByEvenDollar
Else
   distributeByQuantity (multiUserDistCount)
End If

 Screen.MousePointer = vbNormal
 
 'Calls frmUserReassign
If lstOpenAccounts.SelCount > 0 Then
   frmUserReAssign.AcctCol = Nothing
   frmUserReAssign.AcctCol = AcctsToDist
   Set Criteria.AccountCollection = Nothing
   frmUserReAssign.Show vbModal
   lstOpenAccounts.Clear
   cmdSelectAll.Caption = "Select All"
End If
End Sub
---------------------------------------------------------------
Public Function FindSelectedListBoxItems(LstBx As Control) As Collection
 Dim i As Long
 Dim colCollection As New Collection
 
 For i = (LstBx.ListCount - 1) To 0 Step -1
     If LstBx.Selected(i) Then
         colCollection.Add LstBx.List(i)
     End If
 Next
 Set FindSelectedListBoxItems = colCollection
 Set colCollection = Nothing 'New Collection
End Function

---------------------------------------------------------------
Private Sub distributeByQuantity(multiUserDistCount As Integer)
Dim distributeTo As Collection, acctsToDistribute As Collection
Dim i As Integer, c As Integer, x As Integer, j As Integer
Dim account As String, targetAIdIndex As Integer

'Calls function in modProcessingFunctions
Set distributeTo = FindSelectedListBoxItems(lstDist)
Set acctsToDistribute = FindSelectedListBoxItems(lstOpenAccounts)

'Used to maintain correct user
x = 1
j = 1

Set AcctsToDist = New Collection

Select Case multiUserDistCount
   Case 0
       For i = 1 To acctsToDistribute.Count
           account = acctsToDistribute(i)
           targetAIdIndex = 0
           'get acct location in criteria.accountcollection
           Do While targetAIdIndex <= lstOpenAccounts.ListCount
               If lstOpenAccounts.List(targetAIdIndex) = account Then
                   'set j = collection index
                   j = targetAIdIndex + 1
                   'exit while loop
                   targetAIdIndex = lstOpenAccounts.ListCount
               End If
               targetAIdIndex = targetAIdIndex + 1
           Loop
           'save old assign_id and set new one

           Criteria.AccountCollection(j).Old_Assign_Id = Criteria.AccountCollection(j).Assign_ID
           Criteria.AccountCollection(j).Assign_ID = Trim(distributeTo(1))
           AcctsToDist.Add (Criteria.AccountCollection(j))
       Next
   Case Else
           'Used to verify each user receives the same amt
           c = 0
           'distribute each account
           
                   
       For i = 1 To acctsToDistribute.Count
               'true when c = multiuserdistcount, goes to next user
               If multiUserDistCount = c - 1 Then
                   c = 0
                   x = x + 1
               End If
               account = acctsToDistribute(i)
               targetAIdIndex = 0
               'get acct location in criteria.accountcollection
               Do While targetAIdIndex < lstOpenAccounts.ListCount
                   If lstOpenAccounts.List(targetAIdIndex) = account Then
                       'set j = collection index
                       j = targetAIdIndex + 1
                       'exit while loop
                       targetAIdIndex = lstOpenAccounts.ListCount
                   End If
                   targetAIdIndex = targetAIdIndex + 1
               Loop
             
               Criteria.AccountCollection(j).Old_Assign_Id = Criteria.AccountCollection(j).Assign_ID
               Criteria.AccountCollection(j).Assign_ID = Trim(distributeTo(x))
               
               AcctsToDist.Add Criteria.AccountCollection(j)
               'Adds account count to user
               c = c + 1
       Next
End Select


Answer
Your algo seems un-necessarily complex and illegible, I would suggest the following:

lets assume we have TotalItemsToDistribute and TotalItemsToDistributeOver

then:
TotalItemsPerDistribution = TotalItemsToDistribute/TotalItemsToDistributeOver
e.g.
TotalItemsPerDistribution = 150/2 = 75

Then:
For each Distribution in DistributionList
  TempCount = TOtalItemsPerDistrubution
  For each Iterator = 1 to TempCount
      Get FirstItem in ItemsList
      Assign FirsItem to Distribution
      Remove FirstItem from List
  end for
end for

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.