Results 1 to 14 of 14

Thread: Delete rows if

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Delete rows if

    Hello all.

    I need your help with a code.

    I would like to remove rows in my MSFlexgrid: If Form13.MSFlexGrid1.TextMatrix(i, 7) <> Form12.Com_window.Text Then

    I only want to remove rows that match the criteria from the Form12.Com_window.Text.

    Code:
    Private Sub cmdApply4_Click()
    Dim i As Integer, j As Integer, fgRowRemove%
    For i = Form13.MSFlexGrid1.Rows To 1 Step -1
    
       If Form13.MSFlexGrid1.TextMatrix(i, 8) <> Form12.Com_window.Text Then
               fgRemoveRow Form13.MSFlexGrid1, i
       End If
    Next i
    
    End Sub
    With this code, i have an error:Sub or function not define.

    How can i fix that please?

    Thanks for your help

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Delete rows if

    Code:
    Private Sub cmdApply4_Click()
    Dim i As Integer, j As Integer, fgRowRemove&#37;
    For i = Form13.MSFlexGrid1.Rows To 1 Step -1
       If Form13.MSFlexGrid1.TextMatrix(i, 8) <> Form12.Com_window.Text Then
               Form13.MSFlexGrid1.RemoveItem ( i ) 
      End If
    Next i
    
    End Sub
    JG

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Delete rows if

    Hello jggtz.

    I've just try it and now, i have another error.

    runtime error '381' subscript out of range

    Code:
    If Form13.MSFlexGrid1.TextMatrix(i, 8) <> Form12.Com_window.Text Then
    Do you know why?

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Delete rows if

    Sorry, didn't see it, You can't remove all the rows
    Code:
    Private Sub cmdApply4_Click()
    Dim i As Integer, j As Integer, fgRowRemove&#37;
    For i = Form13.MSFlexGrid1.Rows To 2 Step -1
       If Form13.MSFlexGrid1.TextMatrix(i, 8) <> Form12.Com_window.Text Then
               Form13.MSFlexGrid1.RemoveItem ( i ) 
      End If
    Next i
    
    End Sub
    Last edited by jggtz; Mar 7th, 2010 at 07:52 PM.

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Delete rows if

    If you have a large number of rows to delete, using a For/Next loop could take some serious time and hang your application.

    As an alternative, you can simply reduce the number of rows by resetting the control's Rows property. Normally, this property returns the total number of rows in the grid. However, you can also use it to remove rows. For instance, if your flexgrid currently contained 15 rows, and you wanted to reduce them to 10, you could simply issue the following code statement:
    Code:
    MSFlexGrid1.Rows = 9

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Delete rows if

    Hello,

    Sorry but still the same error on:
    If Form13.MSFlexGrid1.TextMatrix(i, 8) <> Form12.Com_window.Text Then

    runtime error '381' subscript out of range

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Delete rows if

    It doesn't matter if it take a long time. I have about 1000 rows.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Delete rows if

    Try this

    Code:
    Private Sub cmdApply4_Click()
        Dim i As Long
        
        With Form13.MSFlexGrid1
            For i = .Rows To 1 Step -1
                If .TextMatrix(i, 8) <> Form12.Com_window.Text Then
                    If (.Rows > .FixedRows + 1) Then
                        .RemoveItem (i)
                    Else
                        .Rows = .FixedRows
                    End If
                End If
            Next i
        End With
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Delete rows if

    No chances. Still the same error.

    I dont understand why.

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Delete rows if

    Show me your project...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Delete rows if

    Like with many things in programming, the first item in a FlexGrid is 0, and 1 is the second, etc.

    The .Rows property returns the amount of rows, which is 1 higher than the last row (when there are 2 rows, they will be 0 and 1).

    As such, this is probably what you want:
    Code:
            For i = .Rows -1 To 0 Step -1

  12. #12
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Delete rows if

    AFAIK, You can't remove all the rows
    You cant't remove fixed row and the last row
    As I posted
    Code:
    For i = Form13.MSFlexGrid1.Rows To 2 Step -1
    As koolsid posted
    Code:
    If (.Rows > .FixedRows + 1) Then

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Delete rows if

    Perfect thanks.

    Now it works with
    Code:
        For i = .Rows -1 To 0 Step -1

  14. #14
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Delete rows if

    If I try this code :
    Code:
    For I = .Rows - 1 To 0 Step -1
        .RemoveItem (I)
    Next I
    I get
    You Can't Remove the last non fixed row


    JG

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width