Page 1 of 2 12 LastLast
Results 1 to 40 of 42

Thread: Database record search using ADO [RESOLVED]

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Resolved Database record search using ADO [RESOLVED]

    Hi,
    I need a form to allow users to type a search query into any textbox linked to a record. I need the search results to be displayed in an MSFlexGrid. The user needs to be able to search in a single record or in multiple records. If someone could point me in the right direction or better still show me some example code it would be greatly appreciated. Also could some tell me how to populate the FlexGrid with the results once the search has finished.
    I hope what I am trying to do makes sense.
    Thanks in advance.
    Attached Images Attached Images  
    Last edited by hscott; Apr 11th, 2005 at 04:31 PM. Reason: Resolved

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Well...
    You could build a dynamic query on the fly:
    VB Code:
    1. Dim strSQL as string, strWhere as string
    2.  
    3. 'basic SQL
    4. strSQL = "SELECT * FROM myTable"
    5.  
    6. 'now create a WHERE clause
    7. If txtSampleNo.Text <> vbNullstring then
    8.     strWhere = strWhere & " AND SampleNum = '" & txtSampleNo.Text & "'"
    9. End If
    10. If txtLocation.Text <> vbNullstring then
    11.     strWhere = strWhere & " AND Location = '" & txtLocation.Text & "'"
    12. End If
    13. 'etc. etc.
    14.  
    15. 'append the WHERE clause (if any) to the basic SQL
    16. if strWhere <> vbNullString then
    17.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 4)
    18. End If
    19.  
    20. 'fire your query here...

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    Thanks for the suggestion. I now have the following code, but have no idea what to do next. I am new to VB and SQL, I have just learnt the basics and things I need for this project, so please explain thinks as simply as possible. Thanks very much.

    VB Code:
    1. Private Sub CmdSearch_Click()
    2. Dim strSQL As String, strWhere As String
    3.  
    4. strSQL = "SELECT * FROM Asbestos"
    5.  
    6. If Text1.Text <> vbNullString Then
    7.     strWhere = strWhere & " AND SampleNumber = '" & Text1.Text & "*"
    8. End If
    9. If Text2.Text <> vbNullString Then
    10.     strWhere = strWhere & " AND Location = '" & Text2.Text & "*"
    11. End If
    12. If Text3.Text <> vbNullString Then
    13.     strWhere = strWhere & " AND ProductType = '" & Text3.Text & "*"
    14. End If
    15. If Text4.Text <> vbNullString Then
    16.     strWhere = strWhere & " AND AsbestosType = '" & Text4.Text & "*"
    17. End If
    18. If Text5.Text <> vbNullString Then
    19.     strWhere = strWhere & " AND LocationInUnit = '" & Text5.Text & "*"
    20. End If
    21. If Text6.Text <> vbNullString Then
    22.     strWhere = strWhere & " AND PBCJobNumber = '" & Text6.Text & "*"
    23. End If
    24. If Text7.Text <> vbNullString Then
    25.     strWhere = strWhere & " AND WorkDone = '" & Text7.Text & "*"
    26. End If
    27. If Text8.Text <> vbNullString Then
    28.     strWhere = strWhere & " AND DateOfCompletion = '" & Text8.Text & "*"
    29. End If
    30. If Text9.Text <> vbNullString Then
    31.     strWhere = strWhere & " AND AirTestCertificateNumber = '" & Text9.Text & "*"
    32. End If
    33. If Text10.Text <> vbNullString Then
    34.     strWhere = strWhere & " AND AssessmentScoreMaterial = '" & Text10.Text & "*"
    35. End If
    36. If Text11.Text <> vbNullString Then
    37.     strWhere = strWhere & " AND AssessmentScorePriority = '" & Text11.Text & "*"
    38. End If
    39. If Text12.Text <> vbNullString Then
    40.     strWhere = strWhere & " AND AssessmentScoreTotal = '" & Text12.Text & "*"
    41. End If
    42.  
    43. If strWhere <> vbNullString Then
    44.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 4)
    45.  
    46. End If
    47. End Sub

  4. #4
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Something like this will get you started:
    VB Code:
    1. Dim cnnExample As ADODB.Connection
    2.     Dim rstExample As ADODB.Recordset
    3.    
    4.     'create and open the connection
    5.     Set cnnExample = New ADODB.Connection
    6.     With cnnExample
    7.         .ConnectionString = "blah"
    8.         .Open
    9.         'get the recordset
    10.         Set rstExample = New ADODB.Recordset
    11.         Set rstExample = cnnExample.Execute(strSQL)
    12.         'disconnect the recordset
    13.         Set rstExample.ActiveConnection = Nothing
    14.         'drop the connection
    15.         .Close
    16.     End With
    17.    
    18.     Me.MSFlexGrid1.DataSource = rstExample
    I'm just going into a meeting right now... I'll come back and check how you're getting on in a bit..!

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    Thanks very much for your time and patience. I now have the following code.

    VB Code:
    1. Option Explicit
    2. Private rsSearch As ADODB.Recordset
    3. Private cn As ADODB.Connection
    4.  
    5. Private Sub Form5_Load()
    6. Set cn = New ADODB.Connection
    7. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    8. cn.Open
    9. Set rsSearch = New ADODB.Recordset
    10. Set rsSearch = cn.Execute(strSQL)
    11. Set rsSearch.ActiveConnection = Nothing
    12. cn.Close
    13. Me.MSFlexGrid1.DataSource = rsSearch
    14. End Sub
    15.  
    16. Private Sub CmdSearch_Click()
    17. Dim strSQL As String, strWhere As String
    18.  
    19. strSQL = "SELECT * FROM Asbestos"
    20.  
    21. If Text1.Text <> vbNullString Then
    22.     strWhere = strWhere & " AND SampleNumber = '" & Text1.Text & "*"
    23. End If
    24. If Text2.Text <> vbNullString Then
    25.     strWhere = strWhere & " AND Location = '" & Text2.Text & "*"
    26. End If
    27. If Text3.Text <> vbNullString Then
    28.     strWhere = strWhere & " AND ProductType = '" & Text3.Text & "*"
    29. End If
    30. If Text4.Text <> vbNullString Then
    31.     strWhere = strWhere & " AND AsbestosType = '" & Text4.Text & "*"
    32. End If
    33. If Text5.Text <> vbNullString Then
    34.     strWhere = strWhere & " AND LocationInUnit = '" & Text5.Text & "*"
    35. End If
    36. If Text6.Text <> vbNullString Then
    37.     strWhere = strWhere & " AND PBCJobNumber = '" & Text6.Text & "*"
    38. End If
    39. If Text7.Text <> vbNullString Then
    40.     strWhere = strWhere & " AND WorkDone = '" & Text7.Text & "*"
    41. End If
    42. If Text8.Text <> vbNullString Then
    43.     strWhere = strWhere & " AND DateOfCompletion = '" & Text8.Text & "*"
    44. End If
    45. If Text9.Text <> vbNullString Then
    46.     strWhere = strWhere & " AND AirTestCertificateNumber = '" & Text9.Text & "*"
    47. End If
    48. If Text10.Text <> vbNullString Then
    49.     strWhere = strWhere & " AND AssessmentScoreMaterial = '" & Text10.Text & "*"
    50. End If
    51. If Text11.Text <> vbNullString Then
    52.     strWhere = strWhere & " AND AssessmentScorePriority = '" & Text11.Text & "*"
    53. End If
    54. If Text12.Text <> vbNullString Then
    55.     strWhere = strWhere & " AND AssessmentScoreTotal = '" & Text12.Text & "*"
    56. End If
    57.  
    58. If strWhere <> vbNullString Then
    59.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 4)
    60. End If
    61. End Sub
    62.  
    63. Private Sub Form5_Unload(Cancel As Integer)
    64. rsSearch.Close
    65. cn.Close
    66. Set rsSearch = Nothing
    67. Set cn = Nothing
    68. End Sub

    When I type a query into the box and click on the search button, nothing happens. Can anyone see any errors in my code?

    Also, I am not sure what this does:

    VB Code:
    1. If strWhere <> vbNullString Then
    2.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 4)
    3. End If

    Can anyone explain this?
    Thanks very much.

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    No problem. Sorry I rushed the last post a bit..!

    The reason that nothing happens is that you need to do the recordset opening bit after building your strSQL string - your snippet just does it on the loading of the form.

    The
    VB Code:
    1. If strWhere <> vbNullString Then
    2.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 4)
    3. End If
    bit checks to see if there is a WHERE clause and if so, adds it to the SELECT... string to create your customised query. (Oh, and the Mid$ is to chop off the first AND as we don't need it.)

  7. #7
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Oh. And I just noticed you are using Access. You'll need to tweak your code a bit. Sorry.
    VB Code:
    1. If Text11.Text <> vbNullString Then
    2.     strWhere = strWhere & " AND AssessmentScorePriority [B][COLOR=Red]LIKE[/COLOR][/B] [COLOR=Red]""[/COLOR]" & Text11.Text & "*[COLOR=Red]""[/COLOR]"
    3. End If

    - Access uses double quotes as string delimiters.
    - You'll need LIKE if you want to use wildcards

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    I am now getting the following error when I click search:
    The error is on line:
    VB Code:
    1. Set rsSearch = cn.Execute(strSQL)
    This happens when I enter something in any of the boxes and click search.
    Attached Images Attached Images  

  9. #9
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Sorry I messed up. 1 character out.
    Try
    VB Code:
    1. strSQL = strSQL & " WHERE " & Mid$(strWhere, [COLOR=Red]5[/COLOR])

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    I tried that. I now get a different error message on the same line.
    Attached Images Attached Images  

  11. #11
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Hmmmm. I'm not doing so well here.
    Can you do a
    VB Code:
    1. Debug.Print strSQL
    and post the SQL string?

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    SELECT * FROM Asbestos WHERE AsbestosType LIKE "Amosite*"

  13. #13
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Looks good to me. I've created a dummy database and it runs here OK.
    Have you tried pasting the SQL into an Access query and running it there?

    Sorry in advance for this question... but you do have a column called AsbestosType in your table don't you?

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    Now I feel stupid, the AsbestosType has a space in my database.
    I have changed my code to match the table but I am now getting the original error message from above.
    Attached Images Attached Images  

  15. #15
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    No probs. We've all been there
    You'll need to either remove the spaces in your column names, or enclose them in square brackets thus: [Asbestos type], otherwise Access (or most other DBs) won't which is the column name. You need to do the same if you use any 'reserved' words (i.e. words that Access - or SQL - has a different meaning for). In the long run it is best not to use spaces or reserved words in column names.


  16. #16

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    I am now getting a different error message on line:
    VB Code:
    1. Set rsSearch.ActiveConnection = Nothing
    Attached Images Attached Images  

  17. #17
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Sorry - juts me trying to be clever (always a bad idea) and use disconnected recordsets (you can read about those here) without doing all the necessary things like using client-side cursors etc.
    You can safely comment out/delete that line.

  18. #18

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    I deleted the line, it fixed that error. I now have a new error when I click search. The error is on the following line:
    VB Code:
    1. Me.MSFlexGrid1.DataSource = rsSearch
    Attached Images Attached Images  

  19. #19
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    I'm really not doing very well here am I?
    I'll look into how you bind a flex grid to a recordset and get back to you...
    PP

  20. #20
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    OK. Looks like you can't do it.
    You can with a hierarchical flex grid, but not a vanilla flex grid.
    VB Code:
    1. Set Me.MSHFlexGrid1.Recordset = rsSearch

    You can do it with the Flex Grid if you loop through the recordset like this:
    VB Code:
    1. Option Explicit
    2. Private rsSearch As ADODB.Recordset
    3. Private cn As ADODB.Connection
    4.  
    5. Private Function GetRecordset(ByVal strSQL As String) As ADODB.Recordset
    6.     Set cn = New ADODB.Connection
    7.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    8.     cn.CursorLocation = adUseClient
    9.     cn.Open
    10.     Set rsSearch = New ADODB.Recordset
    11.     rsSearch.CursorLocation = adUseClient
    12.     Set rsSearch = cn.Execute(strSQL)
    13.     Set rsSearch.ActiveConnection = Nothing
    14.     cn.Close
    15.     Set GetRecordset = rsSearch
    16. End Function
    17.  
    18. Private Sub LoadTheGrid(ByVal strSQL As String)
    19.     Dim rs As ADODB.Recordset
    20.     Dim iCol As Integer, iRow As Integer
    21.     Dim fld As ADODB.Field
    22.    
    23.     Set rs = GetRecordset(strSQL)
    24.     iRow = 0
    25.     With rs
    26.         Me.MSFlexGrid1.Rows = rs.RecordCount
    27.         Me.MSFlexGrid1.Cols = rs.Fields.Count
    28.         Do Until .EOF
    29.             iCol = 0
    30.             For Each fld In .Fields
    31.                 Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    32.                 iCol = iCol + 1
    33.             Next fld
    34.             iRow = iRow + 1
    35.             .MoveNext
    36.         Loop
    37.     End With
    38. End Sub
    39.  
    40. Private Sub Form_Load()
    41.     LoadTheGrid "SELECT * FROM Asbestos"
    42. End Sub

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    I have entered the above code. And my complete code for this form is now:

    VB Code:
    1. Option Explicit
    2. Private rsSearch As ADODB.Recordset
    3. Private cn As ADODB.Connection
    4.  
    5. Private Sub Form5_Load()
    6. LoadTheGrid "SELECT * FROM Asbestos"
    7. End Sub
    8.  
    9. Private Function GetRecordset(ByVal strSQL As String) As ADODB.Recordset
    10.     Set cn = New ADODB.Connection
    11.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    12.     cn.CursorLocation = adUseClient
    13.     cn.Open
    14.     Set rsSearch = New ADODB.Recordset
    15.     rsSearch.CursorLocation = adUseClient
    16.     Set rsSearch = cn.Execute(strSQL)
    17.     Set rsSearch.ActiveConnection = Nothing
    18.     cn.Close
    19.     Set GetRecordset = rsSearch
    20. End Function
    21.  
    22. Private Sub LoadTheGrid(ByVal strSQL As String)
    23.     Dim rs As ADODB.Recordset
    24.     Dim iCol As Integer, iRow As Integer
    25.     Dim fld As ADODB.Field
    26.    
    27.     Set rs = GetRecordset(strSQL)
    28.     iRow = 0
    29.     With rs
    30.         Me.MSFlexGrid1.Rows = rs.RecordCount
    31.         Me.MSFlexGrid1.Cols = rs.Fields.Count
    32.         Do Until .EOF
    33.             iCol = 0
    34.             For Each fld In .Fields
    35.                 Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    36.                 iCol = iCol + 1
    37.             Next fld
    38.             iRow = iRow + 1
    39.             .MoveNext
    40.         Loop
    41.     End With
    42. End Sub
    43.  
    44.  
    45. Private Sub CmdSearch_Click()
    46. Dim strSQL As String, strWhere As String
    47.  
    48. strSQL = "SELECT * FROM Asbestos"
    49.  
    50. If Text1.Text <> vbNullString Then
    51.     strWhere = strWhere & " AND [Sample Number] LIKE """ & Text1.Text & "*"""
    52. End If
    53. If Text2.Text <> vbNullString Then
    54.     strWhere = strWhere & " AND [Location] LIKE """ & Text2.Text & "*"""
    55. End If
    56. If Text3.Text <> vbNullString Then
    57.     strWhere = strWhere & " AND [Product Type] LIKE """ & Text3.Text & "*"""
    58. End If
    59. If Text4.Text <> vbNullString Then
    60.     strWhere = strWhere & " AND [Asbestos Type] LIKE """ & Text4.Text & "*"""
    61. End If
    62. If Text5.Text <> vbNullString Then
    63.     strWhere = strWhere & " AND [Location In Unit] LIKE """ & Text5.Text & "*"""
    64. End If
    65. If Text6.Text <> vbNullString Then
    66.     strWhere = strWhere & " AND [PBC Job Number] =LIKE """ & Text6.Text & "*"""
    67. End If
    68. If Text7.Text <> vbNullString Then
    69.     strWhere = strWhere & " AND [Work Done] LIKE """ & Text7.Text & "*"""
    70. End If
    71. If Text8.Text <> vbNullString Then
    72.     strWhere = strWhere & " AND [Date Of Completion] LIKE """ & Text8.Text & "*"""
    73. End If
    74. If Text9.Text <> vbNullString Then
    75.     strWhere = strWhere & " AND [Air Test Certificate Number] LIKE """ & Text9.Text & "*"""
    76. End If
    77. If Text10.Text <> vbNullString Then
    78.     strWhere = strWhere & " AND [Assessment Score - Material] LIKE """ & Text10.Text & "*"""
    79. End If
    80. If Text11.Text <> vbNullString Then
    81.     strWhere = strWhere & " AND [Assessment Score - Priority] LIKE """ & Text11.Text & "*"""
    82. End If
    83. If Text12.Text <> vbNullString Then
    84.     strWhere = strWhere & " AND [Assessment Score - Total] LIKE """ & Text12.Text & "*"""
    85. End If
    86. If strWhere <> vbNullString Then
    87.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    88. End If
    89. Set cn = New ADODB.Connection
    90. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    91. cn.Open
    92. Set rsSearch = New ADODB.Recordset
    93. Set rsSearch = cn.Execute(strSQL)
    94. Set Me.MSFlexGrid1.DataSource = rsSearch
    95. cn.Close
    96. End Sub
    97.  
    98. Private Sub Form5_Unload(Cancel As Integer)
    99. rsSearch.Close
    100. cn.Close
    101. Set rsSearch = Nothing
    102. Set cn = Nothing
    103. End Sub

    I now get a different error message when I click search.
    The error is on the following line:

    VB Code:
    1. Set Me.MSFlexGrid1.DataSource = rsSearch

    Sorry about all the questions. I really appreciate you taking the time to help me.
    Attached Images Attached Images  

  22. #22
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    Sorry - my code was just an example. Try this:
    VB Code:
    1. Private Sub CmdSearch_Click()
    2.     Dim strSQL As String, strWhere As String
    3.    
    4.     strSQL = "SELECT * FROM Asbestos"
    5.    
    6.     If Text1.Text <> vbNullString Then
    7.         strWhere = strWhere & " AND [Sample Number] LIKE """ & Text1.Text & "*"""
    8.     End If
    9.     If Text2.Text <> vbNullString Then
    10.         strWhere = strWhere & " AND [Location] LIKE """ & Text2.Text & "*"""
    11.     End If
    12.     If Text3.Text <> vbNullString Then
    13.         strWhere = strWhere & " AND [Product Type] LIKE """ & Text3.Text & "*"""
    14.     End If
    15.     If Text4.Text <> vbNullString Then
    16.         strWhere = strWhere & " AND [Asbestos Type] LIKE """ & Text4.Text & "*"""
    17.     End If
    18.     If Text5.Text <> vbNullString Then
    19.         strWhere = strWhere & " AND [Location In Unit] LIKE """ & Text5.Text & "*"""
    20.     End If
    21.     If Text6.Text <> vbNullString Then
    22.         strWhere = strWhere & " AND [PBC Job Number] =LIKE """ & Text6.Text & "*"""
    23.     End If
    24.     If Text7.Text <> vbNullString Then
    25.         strWhere = strWhere & " AND [Work Done] LIKE """ & Text7.Text & "*"""
    26.     End If
    27.     If Text8.Text <> vbNullString Then
    28.         strWhere = strWhere & " AND [Date Of Completion] LIKE """ & Text8.Text & "*"""
    29.     End If
    30.     If Text9.Text <> vbNullString Then
    31.         strWhere = strWhere & " AND [Air Test Certificate Number] LIKE """ & Text9.Text & "*"""
    32.     End If
    33.     If Text10.Text <> vbNullString Then
    34.         strWhere = strWhere & " AND [Assessment Score - Material] LIKE """ & Text10.Text & "*"""
    35.     End If
    36.     If Text11.Text <> vbNullString Then
    37.         strWhere = strWhere & " AND [Assessment Score - Priority] LIKE """ & Text11.Text & "*"""
    38.     End If
    39.     If Text12.Text <> vbNullString Then
    40.         strWhere = strWhere & " AND [Assessment Score - Total] LIKE """ & Text12.Text & "*"""
    41.     End If
    42.     If strWhere <> vbNullString Then
    43.         strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    44.     End If
    45.    
    46.     'this will do the grid loading for you - so you can get rid of the other code
    47.     LoadTheGrid strSQL
    48.  
    49. End Sub

  23. #23

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    The FlexGrid now seems to activate when I click search but no records appear.

    VB Code:
    1. Option Explicit
    2. Private rsSearch As ADODB.Recordset
    3. Private cn As ADODB.Connection
    4.  
    5. Private Sub Form5_Load()
    6. LoadTheGrid "SELECT * FROM Asbestos"
    7. End Sub
    8.  
    9. Private Function GetRecordset(ByVal strSQL As String) As ADODB.Recordset
    10. Set cn = New ADODB.Connection
    11. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    12. cn.CursorLocation = adUseClient
    13. cn.Open
    14. Set rsSearch = New ADODB.Recordset
    15. rsSearch.CursorLocation = adUseClient
    16. Set rsSearch = cn.Execute(strSQL)
    17. Set rsSearch.ActiveConnection = Nothing
    18. cn.Close
    19. Set GetRecordset = rsSearch
    20. End Function
    21.  
    22. Private Sub LoadTheGrid(ByVal strSQL As String)
    23. Dim rs As ADODB.Recordset
    24. Dim iCol As Integer, iRow As Integer
    25. Dim fld As ADODB.Field
    26. Set rs = GetRecordset(strSQL)
    27. iRow = 0
    28. Me.MSFlexGrid1.Rows = rs.RecordCount
    29. Me.MSFlexGrid1.Cols = rs.Fields.Count
    30. Do Until rs.EOF
    31. iCol = 0
    32. For Each fld In rs.Fields
    33. Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    34. iCol = iCol + 1
    35. Next fld
    36. iRow = iRow + 1
    37. rs.MoveNext
    38. Loop
    39. End Sub
    40.  
    41. Private Sub CmdSearch_Click()
    42. Dim strSQL As String, strWhere As String
    43. strSQL = "SELECT * FROM Asbestos"
    44. If Text1.Text <> vbNullString Then
    45.     strWhere = strWhere & " AND [Sample Number] LIKE """ & Text1.Text & "*"""
    46. End If
    47. If Text2.Text <> vbNullString Then
    48.     strWhere = strWhere & " AND [Unit Number] LIKE """ & Text2.Text & "*"""
    49. End If
    50. If Text3.Text <> vbNullString Then
    51.     strWhere = strWhere & " AND [Product Type] LIKE """ & Text3.Text & "*"""
    52. End If
    53. If Text4.Text <> vbNullString Then
    54.     strWhere = strWhere & " AND [Asbestos Type] LIKE """ & Text4.Text & "*"""
    55. End If
    56. If Text5.Text <> vbNullString Then
    57.     strWhere = strWhere & " AND [Location In Unit] LIKE """ & Text5.Text & "*"""
    58. End If
    59. If Text6.Text <> vbNullString Then
    60.     strWhere = strWhere & " AND [PBC Job Number] =LIKE """ & Text6.Text & "*"""
    61. End If
    62. If Text7.Text <> vbNullString Then
    63.     strWhere = strWhere & " AND [Work Done] LIKE """ & Text7.Text & "*"""
    64. End If
    65. If Text8.Text <> vbNullString Then
    66.     strWhere = strWhere & " AND [Date Of Completion] LIKE """ & Text8.Text & "*"""
    67. End If
    68. If Text9.Text <> vbNullString Then
    69.     strWhere = strWhere & " AND [Air Test Certificate Number] LIKE """ & Text9.Text & "*"""
    70. End If
    71. If Text10.Text <> vbNullString Then
    72.     strWhere = strWhere & " AND [Assessment Score - Material] LIKE """ & Text10.Text & "*"""
    73. End If
    74. If Text11.Text <> vbNullString Then
    75.     strWhere = strWhere & " AND [Assessment Score - Priority] LIKE """ & Text11.Text & "*"""
    76. End If
    77. If Text12.Text <> vbNullString Then
    78.     strWhere = strWhere & " AND [Assessment Score - Total] LIKE """ & Text12.Text & "*"""
    79. End If
    80. If strWhere <> vbNullString Then
    81.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    82. End If
    83. LoadTheGrid (strSQL)
    84. End Sub
    85.  
    86. Private Sub Form5_Unload(Cancel As Integer)
    87. rsSearch.Close
    88. cn.Close
    89. Set rsSearch = Nothing
    90. Set cn = Nothing
    91. End Sub
    Attached Images Attached Images  

  24. #24
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    OK. Looks like we're not getting any records returned.
    If you put a breakpoint in LoadTheGrid, how many rows are returned (what does rs.RecordCount contain?).

  25. #25

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Database record search using ADO

    rs.RecordCount = 0

  26. #26
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO

    That'll be it then.
    Have you tried pasting the query into Access again?
    Could you post it here too?
    Would you expect to get records for that criterion? How about if you remove the search text?

  27. #27

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Resolved Re: Database record search using ADO

    !!! IT WORKS !!!!
    I have just realised my mistake. As I am using ADO I should have used % instead of * in my wildcard searches.
    e.g.
    VB Code:
    1. strWhere = strWhere & " AND [Assessment Score - Total] LIKE """ & Text12.Text & "%"""
    Thanks very much for your help PilgrimPete.

  28. #28
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    No probs.
    Sorry it took so long!

  29. #29
    New Member Pace's Avatar
    Join Date
    Oct 2004
    Location
    Manchester
    Posts
    15

    Re: Database record search using ADO [RESOLVED]

    lol that was like a mini adventure, well done

  30. #30
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO [RESOLVED]

    HI
    I am looking at your code, I get an error when i run the code with some minor changes. I am using VB and SQL server.
    The error I get is
    Operation not allowed when the object is closed

    The code is
    Option Explicit

    Private rsSearch As ADODB.Recordset
    Private conSQL As ADODB.Connection

    Private Sub frmHandsetSearch_Load()
    LoadTheGrid "SELECT * FROM Handesets"
    End Sub

    Private Function GetRecordset(ByVal strSQL As String) As ADODB.Recordset

    Set conSQL = New ADODB.Connection

    Dim strSQLServer As String
    Dim strSQLDatabaseToOpen As String
    Dim strConnectString As String
    Dim rsRecords As ADODB.Recordset

    strSQLServer = "sfdaqsql1"
    strSQLDatabaseToOpen = "techlog_copy"
    strConnectString = "Provider=sqloledb;" & _
    "Integrated Security=SSPI;" & _
    "Data Source=" & strSQLServer & ";" & _
    "Initial Catalog=" & strSQLDatabaseToOpen & ";"

    conSQL.CursorLocation = adUseServer
    conSQL.Open (strConnectString)

    Set rsSearch = New ADODB.Recordset
    rsSearch.CursorLocation = adUseServer
    Set rsSearch = conSQL.Execute(strSQL)
    rsSearch.Close
    Set rsSearch.ActiveConnection = Nothing
    conSQL.Close
    Set GetRecordset = rsSearch
    End Function

    Private Sub LoadTheGrid(ByVal strSQL As String)
    Dim rs As ADODB.Recordset
    Dim iCol As Integer, iRow As Integer
    Dim fld As ADODB.Field
    Set rs = GetRecordset(strSQL)
    iRow = 0
    Me.MSFlexGrid1.Rows = rs.RecordCount
    Me.MSFlexGrid1.Cols = rs.Fields.Count
    Do Until rs.EOF
    iCol = 0
    For Each fld In rs.Fields
    Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    iCol = iCol + 1
    Next fld
    iRow = iRow + 1
    rs.MoveNext
    Loop
    End Sub

    Private Sub cmdSearch_Click()
    Dim strSQL As String, strWhere As String
    strSQL = "SELECT * FROM Handsets"

    If Text1.Text <> vbNullString Then
    strWhere = strWhere & " AND [Telephia Asset Number] LIKE """ & Text1.Text & "*"""
    End If
    If Text2.Text <> vbNullString Then
    strWhere = strWhere & " AND [ESN] LIKE """ & Text2.Text & "*"""
    End If
    If Text3.Text <> vbNullString Then
    strWhere = strWhere & " AND [MIN] LIKE """ & Text3.Text & "*"""
    End If
    'If Text4.Text <> vbNullString Then
    ' strWhere = strWhere & " AND [Keyword LIKE """ & Text4.Text & "*"""
    'End If
    'If Text5.Text <> vbNullString Then
    ' strWhere = strWhere & " AND [Location In Unit] LIKE """ & Text5.Text & "*"""
    'End If
    LoadTheGrid (strSQL)
    End Sub

    Private Sub Form5_Unload(Cancel As Integer)
    rsSearch.Close
    conSQL.Close
    Set rsSearch = Nothing
    Set conSQL = Nothing
    End Sub

  31. #31
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO

    It is not resolved

  32. #32
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    Where do you get the error?

  33. #33
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO

    Me.MSFlexGrid1.Rows = rs.RecordCount

  34. #34
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    Then I'd guess it is this line in GetRecordset:
    VB Code:
    1. Set rsSearch.ActiveConnection = Nothing
    You can't use a disconnected recordset with a server-side cursor. Try commenting that line out.

  35. #35
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO [RESOLVED]

    I get another error Invalid use of nulls at

    Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value

    Please help

  36. #36
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    Try this:
    VB Code:
    1. if isnull(fld.Value) = true then
    2.     Me.MSFlexGrid1.TextMatrix(iRow, iCol) = vbnullstring
    3. else
    4.     Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    5. end if

  37. #37
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO [RESOLVED]

    It worked, I see data in my flex grid but it gives me all the data in the table. I want it for a specific data as per the filter.
    Also in the flex grid how do I get the column names

  38. #38
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    You missed this bit:
    VB Code:
    1. [color=red]
    2. If strWhere <> vbNullString Then
    3.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    4. End If[/color]
    5. LoadTheGrid (strSQL)

  39. #39
    Member
    Join Date
    May 2005
    Posts
    42

    Re: Database record search using ADO [RESOLVED]

    Just when I thught I was done , I have another error
    "Invalid column name 1234."
    I get the error at
    Set rsSearch = conSQL.Execute(strSQL1)

    I am using 1234 as my ID number

  40. #40
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Database record search using ADO [RESOLVED]

    Can you do a
    VB Code:
    1. debug.print strSQL1
    and post the results?

Page 1 of 2 12 LastLast

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