Edit or Delete Selected Item from MSHFlexGrid in VB6.0 Tips

Edit or Delete Selected Item from MSHFlexGrid in VB6.0 Tips

In Visual Basic, record listing is very important. Whenever you work on database which is related with SQL, MS-Access, Excel, Text etc, you have to consider about record listing in table format. For show data in table list, there are controls to use such as: Listview, Gridview, FlexGrid, MSHFlexGrid.
This is the post where we learn about record editing or deleting in MSHFlexGrid.
It is necessary to consider that at least one record can be exist on this control for edit or delete item. Add record at run time or show record from database.

Change MSHFlexGrid 'Name' with RecMSGrid containing some record.
Items in MSHFlexGrid for edit or delete
Items details
Now, drag some controls from Toolbox to Form1. There is following controls we drag and set its Name and Caption as bellow. Drag Textbox, Labels, Command buttons for further Process.

 TextBox Properties: 
Define TextBox Name as 'CSrno_Txt', 'CProduct_Txt', 'CQuantity_Txt', 'CAmount_Txt'.

 Label Properties:
Define Labels Caption as 'C_Srno:', 'Product:', 'Quantity:', 'Amount:', 'Selected Row:'.

Drag Label6 and set its Name as ‘Row_Lbl’.

 CommandButton Properties:
Define buttons Caption as 'Edit selected Item', 'Delete selected Item' and Name as 'Edit_Cmd', 'Delete_Cmd'.

Control’s adjustment to Form1 as describe in following image.
Control's description for edit or delete item
Control's description
(1). Edit items process:
First of all, select particular item from RecMSGrid, when you select item, the item selected index will show in ‘Row_Lbl’ label and particular selected item will show in related TextBox. For this activity, write code in RecMSGrid's ‘RowColChange()’ events.
Get Selected Row Value in TextBox Code:

Private Sub RecMSGrid_RowColChange()

   With RecMSGrid

      'Put selected index in (Row_Lbl) Label 
       Row_Lbl.Caption = .Row

      'put every cell value in textbox of selected items
      CSrno_Txt.Text = .TextMatrix(RecMSGrid.Row, 0)

      CProduct_Txt.Text = .TextMatrix(RecMSGrid.Row, 1)

      CQuantity_Txt.Text = .TextMatrix(RecMSGrid.Row, 2)

      CAmount_Txt.Text = .TextMatrix(RecMSGrid.Row, 3)

   End With

End Sub

Now, for edit item from this control write code on ‘Edit_Cmd’ command button’s ‘Click()’ events as shown bellow.

Set Text Value in Particular Row Code:

Private Sub Edit_Cmd_Click()

   With RecMSGrid

      'assign index value in integer variable
      Dim rw As Integer

      rw = Val(Row_Lbl.Caption)

     .TextMatrix(rw, 0) = CSrno_Txt.Text

     .TextMatrix(rw, 1) = CProduct_Txt.Text

     .TextMatrix(rw, 2) = CQuantity_Txt.Text

     .TextMatrix(rw, 3) = CAmount_Txt.Text

   End With

   MsgBox "Record Edited Successgully.", vbInformation

End Sub

When you apply this code, the output will view such as bellow picture.
Edit selected item output from MSHFlexGrid
Edit items output
In this way we can edit items from RecMSGrid's.

(2). Delete item Process:
For deleting particular data from RecMSGrid, just write code in command button’s ‘Click()’ event or directly press ‘Delete’ button from keyboard. For delete items, write code on ‘KeyDown(KeyCode As Integer, Shift As Integer)’ event of RecMSGrid.
Press ‘Delete’ button from keyboard.

Private Sub RecMSGrid_KeyDown(KeyCode As Integer, Shift As Integer)

    'KeyCode = 46 define the ‘Delete’ Button of keyboard

    If KeyCode = 46 Then

        'write execution code here

    End if

End Sub

Now, for delete data from this control write code on ‘Delete_Cmd’ command button’s ‘Click()’ event as shown bellow.

Delete Selected Row Code:

Private Sub Delete_Cmd_Click()

    Dim i As Integer

    With RecMSGrid

        'Check if selected record not equal=1
        If  .RowSel <> 1 Then

             'apply loop from selected record to till last record
             For i = Val(Row_Lbl.Caption) To  .Rows - 2

                     'insert next item in previous item
                     .TextMatrix(i, 0) = .TextMatrix(i + 1, 0)

                     .TextMatrix(i, 1) = .TextMatrix(i + 1, 1)

                     .TextMatrix(i, 2) = .TextMatrix(i + 1, 2)

                     .TextMatrix(i, 3) = .TextMatrix(i + 1, 3)

             Next i

            'Decrease every row on every loop
            .Rows = .Rows - 1


            'If we have only one record, apply cleaning of every cell or first record
            .TextMatrix(RecMSGrid.Row, 0) = ""

            .TextMatrix(RecMSGrid.Row, 1) = ""

            .TextMatrix(RecMSGrid.Row, 2) = ""

            .TextMatrix(RecMSGrid.Row, 3) = ""

      End If

   End With

   MsgBox "Record Deleted Successfully.", vbInformation

End Sub

This code will show delete process through following image.
Delete selected item output from MSHFlexGrid
Delete items output
In this way you can delete selected item from MSHFlexGrid.
Items Edit or delete from MSHFlexGrid in vb6.0 easy tips, you can apply all these steps for better work in coding.