Search for data in a MSHFlexGrid
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Search for data in a MSHFlexGrid

  1. #1
    Join Date
    Jun 2012
    Posts
    13

    Search for data in a MSHFlexGrid

    In my form, I have a MSHFlexGrid (fgdCompany) and a Search button.
    What this search button should do is when I enter data into the InputBox, it should search for the entered data and highlight the row that the data was found in green in the MSHFlexGrid. My searching part works great.
    But my problem is, if the entered data is not found, I want it to output a MsgBox saying "Record not found".
    I can't get the piece of code for that function to work. Can anybody help me to get it right?
    Here is the code I am using for the Search button.

    Code:
    Dim xString As String, xRow As Integer, xNext As Integer
     
      cmdSaveNew.Enabled = False
    
      x = InputBox("Enter Company Name : ", "Search")
      If x = "" Then
        MsgBox "No data entered"
        Exit Sub
      Else
        txtSearch = x
      End If
          
      xString = txtSearch
      
      For xNext = 0 To fgdCompany.Rows
      fgdCompany.Row = xNext
      If xNext = fgdCompany.Rows Then
      MsgBox "Record not found"
      Exit Sub
      End If
      xRow = fgdCompany.Row
      If xString = fgdCompany.TextMatrix(xRow, 1) Then
      myRow = fgdCompany.Row
      fgdCompany.TopRow = myRow
      fgdCompany.RowSel = myRow
      Me.fgdCompany.Row = myRow
      For j = 0 To Me.fgdCompany.Cols - 1
      Me.fgdCompany.Col = j
      Me.fgdCompany.CellBackColor = vbGreen
      Next j
      Exit For
      End If
      Next xNext

  2. #2
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,825

    Re: Search for data in a MSHFlexGrid

    Simply dim a boolean variable at the top of your code which will be false by default.

    In your search routine set this variable to True if a match is found
    At the bottom after the search loop is complete check the value of the variable and if false show your message
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2012
    Posts
    13

    Re: Search for data in a MSHFlexGrid

    Quote Originally Posted by DataMiser View Post
    Simply dim a boolean variable at the top of your code which will be false by default.

    In your search routine set this variable to True if a match is found
    At the bottom after the search loop is complete check the value of the variable and if false show your message
    I tried that but it didn't work. Can you please give me the code for it..... I would greatly appreciate the help!!!

  4. #4
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,825

    Re: Search for data in a MSHFlexGrid

    Show the code where you tried it and explain what you mean by didn't work
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Search for data in a MSHFlexGrid

    I think this little piece of code might cause trouble:
    Code:
      For xNext = 0 To fgdCompany.Rows
        fgdCompany.Row = xNext
        If xNext = fgdCompany.Rows Then
          MsgBox "Record not found"
          Exit Sub
        End If
    ...
    You go For xNext=0 To fgdCompany.Rows.
    Then you set fgdCompanyRow = xNext.
    In case xNext has become fgdCompany.Rows caused by not finding a match, this will cause an error.
    The maximum number for fgdCompanyRow is fgdCompanyRows-1.
    So try to run your loop From xNext = 0 To fgdCompany.Rows - 1

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center