Wilder1626
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
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
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
VB6-search.zip
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
ASKER
Hi
I like you code:
This is where i have the problem.
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
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?
ASKER
That would be even better :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
This is perfect
Thanks for all your help. It works great.
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:
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
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
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
:-(
ASKER
hummm, aikimark, let me try what you propose in ID: 41468797
Sorry, i may have been too quick to accept. :-(
Sorry, i may have been too quick to accept. :-(
MSHFlexGrid1.TextMatrix(i,