Solved

Populate array with unique values only

Posted on 2007-04-09
9
214 Views
Last Modified: 2013-12-25
I have a recordset and I have one column that I want to put into an array and use that array later in my program.  It is possible to have duplicates in this column but I only want unique values in this array.  What is the best way to populate my array so I don't get duplicates?

Here is my code to loop through the recordset and add it to the DB.  My code for populating arySiteIdentifiers() is not completed.  Currently it adds all the 'SiteIdentifier' and not just the unique ones.

Do While Not rsReportingUnits.EOF
        sSQL = _
              CStr(AGENCY_ID) & ", " & _
              PrepString(Trim(rsReportingUnits("ProviderNumber") & "")) & ", " & _
              PrepString(Trim(rsReportingUnits("ReportingUnit") & "")) & ", " & _
              PrepString(Trim(rsReportingUnits("UnitName") & "")) & ", " & _
              PrepString(Trim(rsReportingUnits("SiteIdentifier") & "")) & ", " & _
              PrepString(Trim(rsReportingUnits("ServiceType") & "")) & ", " & _
              PrepString(Trim(rsReportingUnits("DMHLocationCode") & "")) & ", " & _
              PrepString(sStartDate) & ", " & _
              "getdate(), " & _
              CStr(nUserID)
        WriteLog (sSQL)
       
        'We need these identifiers to load the clients        
        arySiteIdentifiers(count) = Trim(rsReportingUnits("SiteIdentifier") & "")        
        count = count + 1

      Call oDatabase.ExecuteQuery(sInsertPrefix & sSQL & sInsertSuffix)
     
      rsReportingUnits.MoveNext
   Loop
0
Comment
Question by:huerita37
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18879886
I would use a dictionary or hashtable.

See here for example http://visualbasic.about.com/od/usingvbnet/l/aa071203a.htm

That way you won't have duplicates...
0
 

Author Comment

by:huerita37
ID: 18881832
I am using VB6 so I can't use a hashtable.  I reviewed the link you posted and it doesn't really give me much help for using a dictionary.  I followed a few links and I still didn't get it.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18881918
You posted in VB.Net?

Example of vb6 hashtable:  (which I don't have a lot of familiarity with)
http://www.devx.com/vb2themax/Tip/19307

The dictionary however, is quite simple:

'Create the dictionary:
Dim d: Set d = CreateObject("Scripting.Dictionary")


'Now add items to it...
d.Add "A", "Apple"
d.Add "B", "Bannana"

'...and if you want to 'test' for an item's key before adding it...
If Not d.Exists("A") Then d.Add "A", "Apricot" 'will not be added, since d.Key("A") is already present...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:huerita37
ID: 18882549
I must have selected the wrong checkbox when asking the question.  Sorry about that and thanks for the VB6 stuff.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18882559
No problem...I'm trying to get away from vb6 myself, so I've been learning vb.net for a little over a year now, thus I nkow both, somewhat. :^)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18882672
What could I have added to earn an "A" grade?
0
 

Author Comment

by:huerita37
ID: 18883883
In the past I have given 'A' grades when someone answers my question and goes a little further in explaining.  I reveiwed  the section on grade giving and saw that if the person thoroughly answers your question, which you did for my VB6 problem , I should give you an 'A'.

I know I can have the grade changed but I need to ask a Moderator and I am not sure how to do that.  Any suggestions?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18883926
Thanx - I've posted for a mod to change it.
I appreciate it! :^)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month5 days, 11 hours left to enroll

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question