Solved

Populate array with unique values only

Posted on 2007-04-09
9
211 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

861 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