Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB6 - Search and replace function

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

User generated image
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
Avatar of aikimark
aikimark
Flag of United States of America image

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)
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

Avatar of Wilder1626

ASKER

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.
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  :)
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
In fact, you could iterate through your controls to populate your combobox.
This is perfect

Thanks for all your help. It works great.
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.
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
hummm, aikimark, let me try what you propose in ID: 41468797

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