VB6 - Transfer from MSHFlexgrid to another

Wilder1626
Wilder1626 used Ask the Experts™
on
Hi,
I need your help for what I would like to do bellow.

In my MSHFlexgrid1, I may have a huge volume of rows and columns. But in column 9, I have IDs.
What I need to do is to transfer in MSHFlexxrid3 all rows and columns. But every time a value will show in column 9 that match column 0 from MSHFlexgrid2, when it will transfer into MSHFlexgrid3, it needs to change the value from column 9 only with the SUB_ID in column 1 from MSHFlexgrid2. All the other details must stay the same. So far, this is not a problem.

Where it gets more complicated is if a single value from column 9 may show more than once in column 0 from MSHFlexgrid2. When that happens, it must be clone into a new row so that both SUB_ID are transfer in MSHFlexgrid3. If I have 3 match, then it should clone it 3 times with each SUB_IDs.

Example:
transfert_no1
The value 12 shows twice in MSHFlexgrid2 with 2 different SUB_IDs. So it needs to create a record of each like below:
transfer_no2
ow can i do that?

Thank you for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please supply a sample project.
Hi MartinLiss

Sorry. Forgot to put it the fist time.
Test-duplicate.zip
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I'm at a play and I'll look at it later this evening.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Private Sub cmdConvert_Click()
Dim lngRow1 As Long
Dim lngRow2 As Long
Dim lngRow3 As Long
Dim lngCol As Long
Dim bGrid3Match As Boolean

MSHFlexGrid3.Cols = MSHFlexGrid1.Cols
' Copy the headers
For lngCol = 0 To MSHFlexGrid1.Cols - 1
    MSHFlexGrid3.TextMatrix(0, lngCol) = MSHFlexGrid1.TextMatrix(0, lngCol)
Next

For lngRow1 = 1 To MSHFlexGrid1.Rows - 1
    bGrid3Match = False
    lngRow3 = lngRow3 + 1
    ' First copy all the data from grid1 to grid 3
    If MSHFlexGrid1.TextMatrix(lngRow1, 9) <> "" Then
        If MSHFlexGrid3.Rows = lngRow3 Then
            MSHFlexGrid3.Rows = MSHFlexGrid3.Rows + 1
        End If
        For lngCol = 0 To MSHFlexGrid1.Cols - 1
            MSHFlexGrid3.TextMatrix(lngRow3, lngCol) = MSHFlexGrid1.TextMatrix(lngRow1, lngCol)
        Next
    End If
    For lngRow2 = 1 To MSHFlexGrid2.Rows - 1
        If MSHFlexGrid1.TextMatrix(lngRow1, 9) = MSHFlexGrid2.TextMatrix(lngRow2, 0) Then
            If bGrid3Match Then
                ' copy grid 1 to 3 again in a new row
                lngRow3 = lngRow3 + 1
                If MSHFlexGrid1.TextMatrix(lngRow1, 9) <> "" Then
                    If MSHFlexGrid3.Rows = lngRow3 Then
                        MSHFlexGrid3.Rows = MSHFlexGrid3.Rows + 1
                    End If
                    For lngCol = 0 To MSHFlexGrid1.Cols - 1
                        MSHFlexGrid3.TextMatrix(lngRow3, lngCol) = MSHFlexGrid1.TextMatrix(lngRow1, lngCol)
                    Next
                End If
            End If
            ' There's a match bewtween grid 1 and grid 2 so copy the col 9 value from grid 2 to grid 3
            MSHFlexGrid3.TextMatrix(lngRow3, 9) = MSHFlexGrid2.TextMatrix(lngRow2, 1)
            bGrid3Match = True
        End If
    Next
Next
End Sub

Open in new window

That's exactly what i needed. Thanks again for your help.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial