Solved

Populate array with unique values only

Posted on 2007-04-09
9
209 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now