Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with A MSFlexGrid 1

Status
Not open for further replies.

PeterPong

Technical User
Dec 9, 2006
2
CA
I have appr 4000 company names in a Grid,however many
have been entered several times ( by mistake )
Ex:
Grid1.TextMatrix(1,0)= "abc"
Grid1.TextMatrix(2,0)= "bbc"
Grid1.TextMatrix(3,0)= "ccc"
Grid1.TextMatrix(4,0)= "abc"
Grid1.TextMatrix(5,0)= "ggg"

I want to transfer the company names into a Grid2 but
eliminating all the companie names which are entered
several times in Grid1.
In the above example,"abc" would only be once in Grid2.

Altough I have a code,it it clumsy and I wonder what
the most simple solution would be

Thanks



 
if the names are in a table

load the grid from the Table


SELECT DISTINCT CompanyNames from TableName


this will eliminatee duplicates


 
Conventionally

Private Sub Command1_Click()

Dim i As Integer, j As Integer
Dim found As Boolean
Dim a As String

grid2.Rows = 1
For i = 1 To grid1.Rows - 1
found = False
a$ = grid1.TextMatrix(i, 0)
With grid2
For j = 0 To .Rows - 1
If .TextMatrix(j, 0) = a$ Then
found = True
Exit For
End If
Next
If Not found Then
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = a$
End If
End With
Next

End Sub

HTH Hugh,
 
You could also use the dictionary object to dedupe the list. Here would be the code using Hugh's loop.

Code:
  a = grid1.TextMatrix(i, 0)
  If dict.Exists(a) = False Then
     grid2.TextMatrix(j, 0) = a
      dict.Add a, a
      j = j + 1
  End If

Swi
 
I would go with Peter if you are data binding, if not...

Could be this is just a piece of code you need to run once, or just now and then, to get rid of duplicates in a dataset; if however it is to be run often speed (time spent finding) may be an issue with 4000 rows.

May be your real app uses more than one column of the Grid(s); if however you are only using one column you could use a ListBox in place of Grid2 because that would allow use of a very fast SendMessage(ListBox1.hwnd, LB_FINDSTRING, -1, ByVal StringtoFind) API call.

Suggest you try Swi's code first because it is the shortest yet, but consider that the shortest code is not always the fastest. To use a Dictionary object you will need a project ref into the Microsoft Scripting Runtime and a Dim dict as Dictionary at the top of the code.

regards Hugh,

 
A quick test in the IDE indicates use of dictionary object very much faster than my original code. About 3 seconds vs about 13 for 4000 rows of three characters.

Good one Swi, a star for you.

regards Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top