Solved

Populate array with unique values only

Posted on 2007-04-09
9
208 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

13 Experts available now in Live!

Get 1:1 Help Now