delete single entry from mshflexgrid

imstillalive
01-01-2008, 05:32 AM
hi,

he is an example of the first column of my mshflexgrid

Id Name
1
1
2
3
3
3
4
4
6
8
8
8

i want to delete the records (only in mshflexgrid) based on the occurrence of ID in the above column....say, i want to delete all ID which have occurred less than three times...so that i would get data with ID 3 and 8 only... hope i m clear...any help will be greatly appreciated....

Flyguy
01-01-2008, 06:33 AM
The FlexGrid only displays the data.
If you want to remove some of the data you have to come up with some algoritm to do so.
There is no magical trick.

In the case of having only items shown which occur at least 3 times, you have to loop through all records, count the number of occurences for each ID the walk through the grid (bottom to top) and remove those lines which don't match your criteria.

imstillalive
01-01-2008, 10:44 PM
-------------------

the master
01-01-2008, 11:52 PM
I apologise for how confusing this code is about to be. Maybe there is a better solution but this is the best i can think of


'Delete all items that occur less than 3 times
private sub DeleteItems()

'You need an array of integers
dim Occurences() as integer
dim x as integer

'Redim the array (if you dont then ubound() will fail)
redim occurences(0)

'First loop through each row and count up how many times each number occurs
for x = 0 to FlexGrid.rows-1

'The number will be the index in the array
'Check if the rows number is bigger than the array's ubound()
if val(flexgrid.textmatrix(x,0))>ubound(occurences(0))

'It is too big so you have to make the array bigger
'Use "redim preserve" instead of just "redim" so the data in the array wont be lost
redim preserve occurences(val(flexgrid.textmatrix(x,0)))
end if

'Add 1 to the correct item in the array
occurences(val(flexgrid.textmatrix(x,0)))=occurences(val(flexgrid.text matrix(x,0)))+1
next

'Now the occurences() array knows how many times each number was used
'You have to loop through again to delete the rows
'This row is slightly different. Remember we are deleting rows!
x=0
do while x<flexgrid.rows

'Check if the number of occurences of this rows number is less than 3
if occurences(val(flexgrid.textmatrix(x,0)))<3 then

'It is so delete the row
flexgrid.removeitem x
else

'If you dont delete the row then you have to move to the next one
x=x+1
end if
loop
end sub


Ive not tested it but that should work. For the second loop you can work backwards like Flyguy said instead

Edit: This is a very detailed example ;)

Flyguy
01-02-2008, 01:14 AM
Thats my question....being a newbie i m having very hard time to sort this out...

any rapid solution would be very helpful for me now...

Pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee...

This is NOT a code handout site.
You first have to show your own efforts.
I gave a hint how to approach this problem and you only start to beg :-\

imstillalive
01-02-2008, 03:29 AM
Sorry FLYGUY for being presenting myself in that way....(as being a new in VB, sometimes it is very difficult think of a logic when u are stucked somewhere.... )




Just going through the code of the master :D .... Thanx

imstillalive
01-02-2008, 06:16 AM
getting error "Expected Array" on the following line

if val(flexgrid.textmatrix(x,0))>ubound(occurences(0)) Then

the master
01-02-2008, 06:31 AM
Woops. Thats a typo. The correct code is the following


if val(flexgrid.textmatrix(x,0))>ubound(occurences()) Then

imstillalive
01-03-2008, 12:05 AM
Worked.............Thanx for ur help....:D :D :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum