Results 1 to 7 of 7

Thread: How to search for a certain record in a MSHFlexGrid?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    41

    How to search for a certain record in a MSHFlexGrid?

    I have a MSHFlexGrid that gets all records via a recordset from a database.
    I want to add a search function so that it would search for the data entered by the user on to the search textbox. But it should search for the data only in a certain column in the MSHFlexGrid.
    Can anyone please give me the code for this?

    And I'm very sorry if the question is not clear. If it is not, please inform me. I will do my best to make it more understandable.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: How to search for a certain record in a MSHFlexGrid?

    Try this....

    I used two text boxes (one for search string, and one for identifying which column you want to search on), and a command button.
    Hope this answers your question. Basically, you set the grid column, then check each row (remember, grids have columns and rows starting at 0).

    Private Sub MSHFlexgridAlignment()

    MSFlexGrid1.Clear
    MSFlexGrid1.Rows = 5

    MSFlexGrid1.Cols = 5

    ''' MSFlexGrid1.ColWidth(0) = 500
    ''' MSFlexGrid1.ColWidth(1) = 2800
    ''' MSFlexGrid1.ColAlignment(0) = 1
    MSFlexGrid1.Row = 1
    MSFlexGrid1.Col = 1
    MSFlexGrid1.Text = "BROWN"
    MSFlexGrid1.Row = 2
    MSFlexGrid1.Text = "JONES"
    MSFlexGrid1.Row = 3
    MSFlexGrid1.Text = "SMITH"
    '''' MSFlexGrid1.ColAlignment(1) = 4

    End Sub

    Private Sub Command1_Click()
    mySearch = Trim(txtSearch.Text) '''set search string by typing in string in txtSearch textbox
    myCol = Val(Trim(txtColNum.Text)) '''set column to check by typing in value in txtColNum textbox
    MSFlexGrid1.Col = myCol ''' sets grid column to identified number (remember, columns start with zero)
    For x = 0 To MSFlexGrid1.Rows - 1 '''iterate through each row to find the search string
    If Trim(MSFlexGrid1.Text) = mySearch Then
    myRow = MSFlexGrid1.Row

    MsgBox myRow '''if found, do whatever you need to do then exit sub---you can bring that row to the top of grid easily if that is what you want to do

    Exit Sub
    End If
    Next x

    End Sub

    Private Sub Form_Load()
    Call MSHFlexgridAlignment
    End Sub

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: How to search for a certain record in a MSHFlexGrid?

    oops...left out a critical line.....

    Add this line: MSFlexGrid1.Row = x immediately within the for-loop.

    sorry....

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: How to search for a certain record in a MSHFlexGrid?

    one more thing...to move your found row to the top, use MSFlexGrid1.TopRow = myRow
    note: if you want to do a SECOND search, you will have to START your new search in the row AFTER the one you just found.

    OK--I'm done....hope this answers YOUR question

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    41

    Re: How to search for a certain record in a MSHFlexGrid?

    the code works great!
    but one more small question.
    Is there anyway that i can highlight the row that is found by the search on the MSHFlexGrid?

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: How to search for a certain record in a MSHFlexGrid?

    Absolutely.....use this: (If you are using a fixed row at the top, start Z at 1 instead of 0)

    Put the following code in the command button routine (or whatever routine you use to bring the row to the top)

    '''First part sets BG to whatever color you currently have as cell.backcolors.....I am using vbYellow
    MSFlexGrid1.Col = X
    For Y = 1 To MSFlexGrid1.Rows - 1
    MSFlexGrid1.Row = Y
    MSFlexGrid1.CellBackColor = vbYellow 'assuming vbYellow is original BG color (change as appropriate)
    Next Y
    Next X

    '''Now, set the ONE row (top) to whatever color you like...I am using vbGreen in this example
    For Z = 0 To MSFlexGrid1.Cols - 1
    MSFlexGrid1.Col = Z
    MSFlexGrid1.Row = rownumber1
    MSFlexGrid1.CellBackColor = vbGreen '''Sets back color of your top row to green
    Next Z
    Last edited by SamOscarBrown; Sep 7th, 2012 at 09:31 AM.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    41

    Re: How to search for a certain record in a MSHFlexGrid?

    hi. When I run the program it gives the error "Next without For" and when clicked debug, it highlights the line "Next x"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width