Solved

Populate array with unique values only

Posted on 2007-04-09
9
213 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
Industry Leaders: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

734 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