VB6 - Search and replace function

Wilder1626
Wilder1626 used Ask the Experts™
on
Hi

I have a form with 3 MSHFlexgrid. Each grid are populated.

On the side, i have  2 combobox, one to select what MSHFlexgrid i want to search in, the second if the header column name.

Then, i have the Search value I want to search for and then, the replace Textbox with what i want to replace the value with.

So far, i was using this below code, but this is only for hard coded MSHFlexgrid and column.

I can i update it to adjust based on the the 2 combobox

search value
Private Sub replace_value_Click()
    Dim i As Long
    Dim LastRow As Long
    Dim Found As Boolean

    If Len(Trim(search.Text)) = 0 Then Exit Sub

    For i = 1 To MSHFlexGrid1.Rows - 1
        If search.Text = Mid(MSHFlexGrid1.TextMatrix(i, 2), 1, Len(Trim(search.Text))) Then
            LastRow = i
            Found = True

            If Found = True Then
                MSHFlexGrid1.TextMatrix(i, 2) = replace.Text
                Exit Sub
            Else

            End If
        End If

    Next
End Sub

Open in new window

VB6-search.zip
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
Just add some code that iterates through the first row to find out which column has the text ("testC" in your example).  Set that column into a variable and then replace the "2" in the following expression to your variable name
MSHFlexGrid1.TextMatrix(i, 2)
Top Expert 2014

Commented:
Your search code seems overly complicated.  This should accomplish the same thing.
    For i = 1 To MSHFlexGrid1.Rows - 1
        If search.Text = MSHFlexGrid1.TextMatrix(i, 2) Then
            MSHFlexGrid1.TextMatrix(i, 2) = replace.Text
            Exit Sub
        End If

    Next

Open in new window

Hi

I like you code:
For i = 1 To MSHFlexGrid1.Rows - 1
        If search.Text = MSHFlexGrid1.TextMatrix(i, 2) Then
            MSHFlexGrid1.TextMatrix(i, 2) = replace.Text
            Exit Sub
        End If

    Next

Open in new window

The problem i have is that the MSHFlexhrid is hard coded in the code when i want to use the Combo1_grid_number.text to specify the MSHFlexgrid and also Combo2_col_name to specify the column of the grid.

This is where i have the problem.
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!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Currently you are loading Combo2col_name with all the column names from the three grids. Would you rather have it show just the vales from the grid that selected in grid_number?
That would be even better  :)
Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Try this code. there are some other tweaks I'd make but this is a start.

Private Sub Form_Load()
grid_number.AddItem "MSHFlexgrid1"
grid_number.AddItem "MSHFlexgrid2"
grid_number.AddItem "MSHFlexgrid3"

'new
'Combo2col_name.AddItem "testA"
'Combo2col_name.AddItem "testB"
'Combo2col_name.AddItem "testC"
'Combo2col_name.AddItem "testD"
'Combo2col_name.AddItem "testE"
'Combo2col_name.AddItem "testF"
'Combo2col_name.AddItem "testG"
'Combo2col_name.AddItem "testH"
'Combo2col_name.AddItem "testI"



'HEATHER

MSHFlexGrid1.TextMatrix(0, 0) = "testA"
MSHFlexGrid1.TextMatrix(0, 1) = "testB"
MSHFlexGrid1.TextMatrix(0, 2) = "testC"
MSHFlexGrid1.TextMatrix(0, 3) = "testD"
MSHFlexGrid1.TextMatrix(0, 4) = "testE"


MSHFlexGrid2.TextMatrix(0, 0) = "testC"
MSHFlexGrid2.TextMatrix(0, 1) = "testD"
MSHFlexGrid2.TextMatrix(0, 2) = "testE"
MSHFlexGrid2.TextMatrix(0, 3) = "testF"
MSHFlexGrid2.TextMatrix(0, 4) = "testG"


MSHFlexGrid3.TextMatrix(0, 0) = "testE"
MSHFlexGrid3.TextMatrix(0, 1) = "testF"
MSHFlexGrid3.TextMatrix(0, 2) = "testG"
MSHFlexGrid3.TextMatrix(0, 3) = "testH"
MSHFlexGrid3.TextMatrix(0, 4) = "testI"


MSHFlexGrid1.TextMatrix(1, 0) = "q"
MSHFlexGrid1.TextMatrix(1, 1) = "w"
MSHFlexGrid1.TextMatrix(1, 2) = "e"
MSHFlexGrid1.TextMatrix(1, 3) = "r"
MSHFlexGrid1.TextMatrix(1, 4) = "t"


MSHFlexGrid2.TextMatrix(1, 0) = "y"
MSHFlexGrid2.TextMatrix(1, 1) = "u"
MSHFlexGrid2.TextMatrix(1, 2) = "i"
MSHFlexGrid2.TextMatrix(1, 3) = "o"
MSHFlexGrid2.TextMatrix(1, 4) = "p"


MSHFlexGrid3.TextMatrix(1, 0) = "a"
MSHFlexGrid3.TextMatrix(1, 1) = "s"
MSHFlexGrid3.TextMatrix(1, 2) = "d"
MSHFlexGrid3.TextMatrix(1, 3) = "f"
MSHFlexGrid3.TextMatrix(1, 4) = "g"


MSHFlexGrid1.TextMatrix(2, 0) = "z"
MSHFlexGrid1.TextMatrix(2, 1) = "x"
MSHFlexGrid1.TextMatrix(2, 2) = "c"
MSHFlexGrid1.TextMatrix(2, 3) = "v"
MSHFlexGrid1.TextMatrix(2, 4) = "b"


MSHFlexGrid2.TextMatrix(2, 0) = "n"
MSHFlexGrid2.TextMatrix(2, 1) = "m"
MSHFlexGrid2.TextMatrix(2, 2) = "l"
MSHFlexGrid2.TextMatrix(2, 3) = "k"
MSHFlexGrid2.TextMatrix(2, 4) = "j"


MSHFlexGrid3.TextMatrix(2, 0) = "h"
MSHFlexGrid3.TextMatrix(2, 1) = "g"
MSHFlexGrid3.TextMatrix(2, 2) = "f"
MSHFlexGrid3.TextMatrix(2, 3) = "d"
MSHFlexGrid3.TextMatrix(2, 4) = "s"

MSHFlexGrid1.TextMatrix(3, 0) = "q"
MSHFlexGrid1.TextMatrix(3, 1) = "w"
MSHFlexGrid1.TextMatrix(3, 2) = "e"
MSHFlexGrid1.TextMatrix(3, 3) = "r"
MSHFlexGrid1.TextMatrix(3, 4) = "t"


MSHFlexGrid2.TextMatrix(3, 0) = "y"
MSHFlexGrid2.TextMatrix(3, 1) = "u"
MSHFlexGrid2.TextMatrix(3, 2) = "i"
MSHFlexGrid2.TextMatrix(3, 3) = "o"
MSHFlexGrid2.TextMatrix(3, 4) = "p"


MSHFlexGrid3.TextMatrix(3, 0) = "a"
MSHFlexGrid3.TextMatrix(3, 1) = "s"
MSHFlexGrid3.TextMatrix(3, 2) = "d"
MSHFlexGrid3.TextMatrix(3, 3) = "f"
MSHFlexGrid3.TextMatrix(3, 4) = "g"

'new
grid_number.ListIndex = 0
End Sub

'new
Private Sub FillColumnNames(LI As Integer)

Dim grd As MSHFlexGrid
Dim lngCol As Long

Select Case LI
    Case 0
        Set grd = MSHFlexGrid1
    Case 1
        Set grd = MSHFlexGrid2
    Case 2
        Set grd = MSHFlexGrid3
End Select

Combo2col_name.Clear

For lngCol = 0 To grd.Cols - 1
    Combo2col_name.AddItem grd.TextMatrix(0, lngCol)
Next
Combo2col_name.ListIndex = 0
End Sub

Private Sub grid_number_Click()
'new
FillColumnNames grid_number.ListIndex

End Sub

Private Sub replace_value_Click()
'new essentially rewrote
    Dim i As Long
    Dim LastRow As Long
    Dim Found As Boolean
    Dim grd As MSHFlexGrid
    
    If Len(Trim(search.Text)) = 0 Then Exit Sub
    
    Select Case grid_number.ListIndex
        Case 0
            Set grd = MSHFlexGrid1
        Case 1
            Set grd = MSHFlexGrid2
        Case 2
            Set grd = MSHFlexGrid3
        Case Else
            MsgBox "Please choose a " & Label1.Caption & " to search from"
            Exit Sub
    End Select
    
    For i = 1 To grd.Rows - 1
        If search.Text = Mid(grd.TextMatrix(i, Combo2col_name.ListIndex), 1, Len(Trim(search.Text))) Then
            LastRow = i
            Found = True

            If Found = True Then
                grd.TextMatrix(i, Combo2col_name.ListIndex) = replace.Text
                Exit Sub
            Else

            End If
        End If

    Next
    If Not Found Then
        MsgBox "Not found"
    End If
End Sub

Open in new window

Top Expert 2014

Commented:
Iterate through the Controls collection on the form until you find a control with the matching name.  Then set an control/object variable = that iteration's control and stop the iteration.  In your code, replace the hard-coded name of the control with the variable name you just set.
Top Expert 2014

Commented:
In fact, you could iterate through your controls to populate your combobox.
This is perfect

Thanks for all your help. It works great.
Top Expert 2014

Commented:
You may not even need to iterate the controls in VB6.  You should try referencing the control by name as the index into the
Controls collection.
Example:
Dim ctlSelectedGrid As Control

Set ctlSelectedGrid = Controls(cboGrid_number.Text)

For i = 1 To ctlSelectedGrid.Rows - 1
        If search.Text = ctlSelectedGrid.TextMatrix(i, 2) Then
            ctlSelectedGrid.TextMatrix(i, 2) = replace.Text
            Exit Sub
        End If

    Next

Open in new window

Again, the "2" will be replaced with the selected column number as I mentioned earlier.
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 MVE 2015
              Experts-Exchange Top Expert Visual Basic Classic 2012 to 2015
Top Expert 2014

Commented:
:-(
hummm, aikimark, let me try what you propose in ID: 41468797

Sorry, i may have been too quick to accept. :-(

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