Results 1 to 25 of 25

Thread: Generate a report from search results

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Generate a report from search results

    Hi
    How can I generate a report from the results of a record search.
    The results are currently displayed in an MSFlexGrid.

    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. If rs.RecordCount = 0 Then
    30. MsgBox ("No records meet your search query.")
    31. End If
    32. Me.MSFlexGrid1.Cols = rs.Fields.Count
    33. Do Until rs.EOF
    34. iCol = 0
    35. For Each fld In rs.Fields
    36. Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    37. iCol = iCol + 1
    38. Next fld
    39. iRow = iRow + 1
    40. rs.MoveNext
    41. Loop
    42. End Sub
    43.  
    44. Private Sub CmdBack_Click()
    45. frmEditAsbestos.Show
    46. Form5.Hide
    47. End Sub
    48.  
    49. Private Sub CmdSearch_Click()
    50. Dim strSQL As String, strWhere As String
    51. strSQL = "SELECT * FROM Asbestos"
    52. If Text1.Text <> vbNullString Then
    53.     strWhere = strWhere & " AND [Sample Number] LIKE ""%" & Text1.Text & "%"""
    54. End If
    55. If Text2.Text <> vbNullString Then
    56.     strWhere = strWhere & " AND [Unit Number] LIKE ""%" & Text2.Text & "%"""
    57. End If
    58. If Text3.Text <> vbNullString Then
    59.     strWhere = strWhere & " AND [Product Type] LIKE ""%" & Text3.Text & "%"""
    60. End If
    61. If Text4.Text <> vbNullString Then
    62.     strWhere = strWhere & " AND [Asbestos Type] LIKE ""%" & Text4.Text & "%"""
    63. End If
    64. If Text5.Text <> vbNullString Then
    65.     strWhere = strWhere & " AND [Location In Unit] LIKE ""%" & Text5.Text & "%"""
    66. End If
    67. If Text6.Text <> vbNullString Then
    68.     strWhere = strWhere & " AND [PBC Job Number] LIKE ""%" & Text6.Text & "%"""
    69. End If
    70. If Text7.Text <> vbNullString Then
    71.     strWhere = strWhere & " AND [Work Done] LIKE ""%" & Text7.Text & "%"""
    72. End If
    73. If Text8.Text <> vbNullString Then
    74.     strWhere = strWhere & " AND [Date Of Completion] LIKE ""%" & Text8.Text & "%"""
    75. End If
    76. If Text9.Text <> vbNullString Then
    77.     strWhere = strWhere & " AND [Air Test Certificate Number] LIKE ""%" & Text9.Text & "%"""
    78. End If
    79. If Text10.Text <> vbNullString Then
    80.     strWhere = strWhere & " AND [Assessment Score - Material] LIKE ""%" & Text10.Text & "%"""
    81. End If
    82. If Text11.Text <> vbNullString Then
    83.     strWhere = strWhere & " AND [Assessment Score - Priority] LIKE ""%" & Text11.Text & "%"""
    84. End If
    85. If Text12.Text <> vbNullString Then
    86.     strWhere = strWhere & " AND [Assessment Score - Total] LIKE ""%" & Text12.Text & "%"""
    87. End If
    88. If strWhere <> vbNullString Then
    89.     strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    90. End If
    91. LoadTheGrid (strSQL)
    92. End Sub
    93.  
    94. Private Sub Form5_Unload(Cancel As Integer)
    95. rsSearch.Close
    96. cn.Close
    97. Set rsSearch = Nothing
    98. Set cn = Nothing
    99. End Sub
    Attached Images Attached Images  

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Generate a report from search results

    What type of report generator do you have available? (Like Crystal reports for example.)

    The simplest method would probably be to dump the results of your recordset into a temp Access table, and run a pre-created Access report off of that.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    How would I dump the results into an access file?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Generate a report from search results

    Well, you know what fields are being returned from your query. Create a temp table in an Access database that would store each piece of this information.

    Are you familiar with writing INSERT queries?

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I know how to insert values into a table using;
    INSERT INTO tablename (column1, column2,....)
    VALUES (value1, value2,....)
    But I have no idea how to insert a recordset.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a report from search results

    Why not just use the PRINTER object to print the FLEX GRID data?

    You know the TWIPS size of each flex grid column and can even use the .TEXTWIDTH property to measure the actual text...

    All this allows for some easy printing with the PRINTER object - using the .CURRENTX setting to move from column to column on the paper.

    We have a general routine to print all the flex grids on a form to paper. First it sizes up the data in the flex grids - determines if 8 or 10 fontsize is going to be used - determines if portrait or landscape is going to be used. Even asks if legal paper tray should be used if the data is that big...

    Works nice for us - wasn't all that hard to develop.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Generate a report from search results

    Quote Originally Posted by hscott
    I know how to insert values into a table using;
    INSERT INTO tablename (column1, column2,....)
    VALUES (value1, value2,....)
    But I have no idea how to insert a recordset.
    The exact same way.
    VB Code:
    1. sSQL = "INSERT INTO table (column1, column2, column3) VALUES ('" & adoRs.Fields("column1") & "','" &  adoRs.Fields("column2") & "','" &  adoRs.Fields("column3") & "')"
    Naturally, numeric entries would not have the single quotes around them.

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I now have the following code. The layout is wrong because I keep getting compile errors. Could you please tell me where I have gone wrong.

    VB Code:
    1. Private Sub CmdReport_Click()
    2. Dim strSQL As String
    3. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) "
    4. VALUES ('" & rsSearch.Fields("Sample Number") & "','" & rsSearch.Fields("Unit Number") & "','" & rsSearch.Fields("Product Type") & "','" & rsSearch.Fields("Asbestos Type") & "','" & rsSearch.Fields("Location In Unit") & "','" & rsSearch.Fields("PBC Job Number") & "','" & rsSearch.Fields("Work Done") & "','" & rsSearch.Fields("Date Of Completion") & "','" & rsSearch.Fields("Air Test Certificate Number") & "','" & rsSearch.Fields("Assessment Score - Material") & "','" & rsSearch.Fields("Assessment Score - Priority") & "','" & rsSearch.Fields("Assessment Score - Total") & "')
    5. End Sub

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

    Re: Generate a report from search results

    Firstly, you have no line-continuation...
    VB Code:
    1. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) " [COLOR=Red]& _[/COLOR]
    2. "VALUES ('" & rsSearch.Fields("Sample Number") & "','" & rsSearch.Fields("Unit Number") & "','" & rsSearch.Fields("Product Type") & "','" & rsSearch.Fields("Asbestos Type") & "','" & rsSearch.Fields("Location In Unit") & "','" & rsSearch.Fields("PBC Job Number") & "','" & rsSearch.Fields("Work Done") & "','" & rsSearch.Fields("Date Of Completion") & "','" & rsSearch.Fields("Air Test Certificate Number") & "','" & rsSearch.Fields("Assessment Score - Material") & "','" & rsSearch.Fields("Assessment Score - Priority") & "','" & rsSearch.Fields("Assessment Score - Total") & "')"
    Secondly, you might need to replace each of your single quotes with a pair of double quotes (can't remember if ADO handles that for you - Access certainly uses double quotes natively).

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I now have the following code. I have added the INSERT statement to my CmdSearch_Click() so it copies the results stright into the temp table.
    I get no error messages the data just doesn't appear in the table. Can someone tell me where I am going wrong?

    VB Code:
    1. Private Sub CmdSearch_Click()
    2. Dim strSQL As String, strWhere As String
    3. strSQL = "SELECT * FROM Asbestos"
    4. If Text1.Text <> vbNullString Then
    5. strWhere = strWhere & " AND [Sample Number] LIKE ""%" & Text1.Text & "%"""
    6. End If
    7. If Text2.Text <> vbNullString Then
    8. strWhere = strWhere & " AND [Unit Number] LIKE ""%" & Text2.Text & "%"""
    9. End If
    10. If Text3.Text <> vbNullString Then
    11. strWhere = strWhere & " AND [Product Type] LIKE ""%" & Text3.Text & "%"""
    12. End If
    13. If Text4.Text <> vbNullString Then
    14. strWhere = strWhere & " AND [Asbestos Type] LIKE ""%" & Text4.Text & "%"""
    15. End If
    16. If Text5.Text <> vbNullString Then
    17. strWhere = strWhere & " AND [Location In Unit] LIKE ""%" & Text5.Text & "%"""
    18. End If
    19. If Text6.Text <> vbNullString Then
    20. strWhere = strWhere & " AND [PBC Job Number] LIKE ""%" & Text6.Text & "%"""
    21. End If
    22. If Text7.Text <> vbNullString Then
    23. strWhere = strWhere & " AND [Work Done] LIKE ""%" & Text7.Text & "%"""
    24. End If
    25. If Text8.Text <> vbNullString Then
    26. strWhere = strWhere & " AND [Date Of Completion] LIKE ""%" & Text8.Text & "%"""
    27. End If
    28. If Text9.Text <> vbNullString Then
    29. strWhere = strWhere & " AND [Air Test Certificate Number] LIKE ""%" & Text9.Text & "%"""
    30. End If
    31. If Text10.Text <> vbNullString Then
    32. strWhere = strWhere & " AND [Assessment Score - Material] LIKE ""%" & Text10.Text & "%"""
    33. End If
    34. If Text11.Text <> vbNullString Then
    35. strWhere = strWhere & " AND [Assessment Score - Priority] LIKE ""%" & Text11.Text & "%"""
    36. End If
    37. If Text12.Text <> vbNullString Then
    38. strWhere = strWhere & " AND [Assessment Score - Total] LIKE ""%" & Text12.Text & "%"""
    39. End If
    40. If strWhere <> vbNullString Then
    41. strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    42. End If
    43. LoadTheGrid (strSQL)
    44. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) VALUES ("" & rsSearch.Fields(Sample Number) & "","" & rsSearch.Fields(Unit Number) & "","" & rsSearch.Fields(Product Type) & "","" & rsSearch.Fields(Asbestos Type) & "","" & rsSearch.Fields(Location In Unit) & "","" & rsSearch.Fields(PBC Job Number) & "","" & rsSearch.Fields(Work Done) & "","" & rsSearch.Fields(Date Of Completion) & "","" & rsSearch.Fields(Air Test Certificate Number) & "","" & rsSearch.Fields(Assessment Score - Material) & "","" & rsSearch.Fields(Assessment Score - Priority) & "","" & rsSearch.Fields(Assessment Score - Total) & "")"
    45. End Sub

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Generate a report from search results

    Where you went wrong is nice and simple - you built the SQL but didnt run it

    You should be able to just add this to the line after strSQL = "INSERT INTO..:
    cn.Execute(strSQL)

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I now get the following error when I click the search button.
    Attached Images Attached Images  

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

    Re: Generate a report from search results

    This time you forgot to open the connection...

    You can use a similar function to the GetRecordset one we created yesterday. For example:
    VB Code:
    1. Private Sub DoInsert(ByVal strSQL As String)
    2. Set cn = New ADODB.Connection
    3. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    4. cn.Open
    5. cn.Execute(strSQL)
    6. cn.Close
    7. End Function
    Now all you need to do is call
    VB Code:
    1. DoInsert strSQL
    after you've built your SQL string and you're there.

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I now get the following error on the line:

    VB Code:
    1. cn.Execute(strSQL)
    Attached Images Attached Images  

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

    Re: Generate a report from search results

    That will probably be because you are trying to stuff a string into a numeric field. Check all the data types of your database columns, and if any of them are numeric, remove the surrounding quotes in the INSERT string.

    [edit]
    Oh, hang on. "Criteria expression" implies a WHERE clause, and you don't have one... which cn.Execute(strSQL) is failing - DoInsert or GetRecordset?
    [/edit]

  16. #16

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    DoInsert

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a report from search results

    Just curious - how complex a report are you doing that you are going through all this effort to move data into ACCESS into order to do it. That seems to me like hitting a nail with a sledgehammer!

    The PRINTER object in VB allows for FONTSIZE and FONTSTYLE changes - so you can do large font headings - bold, italics - all with one line commands...

    Looping through the rows and columns of a flexgrid to print is very, very easy.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Generate a report from search results

    OK. Looks like my earlier post wasn't clear enough - sorry!
    You need to double-up your double quotes...

    VB Code:
    1. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], " & _
    2.          "[PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], " & _
    3.          "[Assessment Score - Priority], [Assessment Score - Total]) " & _
    4.          "VALUES (""" & rsSearch.Fields("Sample Number") & """,""" & rsSearch.Fields("Unit Number") & """,""" & _
    5.          rsSearch.Fields("Product Type") & """,""" & rsSearch.Fields("Asbestos Type") & """,""" & _
    6.          rsSearch.Fields("Location In Unit") & """,""" & rsSearch.Fields("PBC Job Number") & """,""" & _
    7.          rsSearch.Fields("Work Done") & """,""" & rsSearch.Fields("Date Of Completion") & """,""" & _
    8.          rsSearch.Fields("Air Test Certificate Number") & """,""" & rsSearch.Fields("Assessment Score - Material") & """,""" & _
    9.          rsSearch.Fields("Assessment Score - Priority") & """,""" & rsSearch.Fields("Assessment Score - Total") & """)"

  19. #19

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    Hi,
    I now get the following error on the following line.

    VB Code:
    1. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) VALUES (""" & rsSearch.Fields("Sample Number") & """,""" & rsSearch.Fields("Unit Number") & """,""" & rsSearch.Fields("Product Type") & """,""" & rsSearch.Fields("Asbestos Type") & """,""" & rsSearch.Fields("Location In Unit") & """,""" & rsSearch.Fields("PBC Job Number") & """,""" & rsSearch.Fields("Work Done") & """,""" & rsSearch.Fields("Date Of Completion") & """,""" & rsSearch.Fields("Air Test Certificate Number") & """,""" & rsSearch.Fields("Assessment Score - Material") & """,""" & rsSearch.Fields("Assessment Score - Priority") & """,""" & rsSearch.Fields("Assessment Score - Total") & """)"

    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 DoInsert(ByVal strSQL As String)
    23. Set cn = New ADODB.Connection
    24. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    25. cn.Open
    26. cn.Execute (strSQL)
    27. cn.Close
    28. End Sub
    29.  
    30. Private Sub LoadTheGrid(ByVal strSQL As String)
    31. Dim rs As ADODB.Recordset
    32. Dim iCol As Integer, iRow As Integer
    33. Dim fld As ADODB.Field
    34. Set rs = GetRecordset(strSQL)
    35. iRow = 0
    36. Me.MSFlexGrid1.Rows = rs.RecordCount
    37. If rs.RecordCount = 0 Then
    38. MsgBox ("No records meet your search query.")
    39. End If
    40. Me.MSFlexGrid1.Cols = rs.Fields.Count
    41. Do Until rs.EOF
    42. iCol = 0
    43. For Each fld In rs.Fields
    44. Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    45. iCol = iCol + 1
    46. Next fld
    47. iRow = iRow + 1
    48. rs.MoveNext
    49. Loop
    50. End Sub
    51.  
    52. Private Sub CmdBack_Click()
    53. frmEditAsbestos.Show
    54. Form5.Hide
    55. End Sub
    56.  
    57. Private Sub CmdSearch_Click()
    58. Dim strSQL As String, strWhere As String
    59. strSQL = "SELECT * FROM Asbestos"
    60. If Text1.Text <> vbNullString Then
    61. strWhere = strWhere & " AND [Sample Number] LIKE ""%" & Text1.Text & "%"""
    62. End If
    63. If Text2.Text <> vbNullString Then
    64. strWhere = strWhere & " AND [Unit Number] LIKE ""%" & Text2.Text & "%"""
    65. End If
    66. If Text3.Text <> vbNullString Then
    67. strWhere = strWhere & " AND [Product Type] LIKE ""%" & Text3.Text & "%"""
    68. End If
    69. If Text4.Text <> vbNullString Then
    70. strWhere = strWhere & " AND [Asbestos Type] LIKE ""%" & Text4.Text & "%"""
    71. End If
    72. If Text5.Text <> vbNullString Then
    73. strWhere = strWhere & " AND [Location In Unit] LIKE ""%" & Text5.Text & "%"""
    74. End If
    75. If Text6.Text <> vbNullString Then
    76. strWhere = strWhere & " AND [PBC Job Number] LIKE ""%" & Text6.Text & "%"""
    77. End If
    78. If Text7.Text <> vbNullString Then
    79. strWhere = strWhere & " AND [Work Done] LIKE ""%" & Text7.Text & "%"""
    80. End If
    81. If Text8.Text <> vbNullString Then
    82. strWhere = strWhere & " AND [Date Of Completion] LIKE ""%" & Text8.Text & "%"""
    83. End If
    84. If Text9.Text <> vbNullString Then
    85. strWhere = strWhere & " AND [Air Test Certificate Number] LIKE ""%" & Text9.Text & "%"""
    86. End If
    87. If Text10.Text <> vbNullString Then
    88. strWhere = strWhere & " AND [Assessment Score - Material] LIKE ""%" & Text10.Text & "%"""
    89. End If
    90. If Text11.Text <> vbNullString Then
    91. strWhere = strWhere & " AND [Assessment Score - Priority] LIKE ""%" & Text11.Text & "%"""
    92. End If
    93. If Text12.Text <> vbNullString Then
    94. strWhere = strWhere & " AND [Assessment Score - Total] LIKE ""%" & Text12.Text & "%"""
    95. End If
    96. If strWhere <> vbNullString Then
    97. strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    98. End If
    99. LoadTheGrid (strSQL)
    100. strSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) VALUES (""" & rsSearch.Fields("Sample Number") & """,""" & rsSearch.Fields("Unit Number") & """,""" & rsSearch.Fields("Product Type") & """,""" & rsSearch.Fields("Asbestos Type") & """,""" & rsSearch.Fields("Location In Unit") & """,""" & rsSearch.Fields("PBC Job Number") & """,""" & rsSearch.Fields("Work Done") & """,""" & rsSearch.Fields("Date Of Completion") & """,""" & rsSearch.Fields("Air Test Certificate Number") & """,""" & rsSearch.Fields("Assessment Score - Material") & """,""" & rsSearch.Fields("Assessment Score - Priority") & """,""" & rsSearch.Fields("Assessment Score - Total") & """)"
    101. DoInsert (strSQL)
    102. End Sub
    103.  
    104. Private Sub Form5_Unload(Cancel As Integer)
    105. rsSearch.Close
    106. cn.Close
    107. Set rsSearch = Nothing
    108. Set cn = Nothing
    109. End Sub
    Attached Images Attached Images  

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

    Re: Generate a report from search results

    OK. That is because your recordset is 'out of scope'.
    You need to do the DoInsert somewhere where your recordset is open - for example in LoadTheGrid.

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    I am still getting the same error message. Could you please explain your previous post in more detail. Thanks very much.

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

    Re: Generate a report from search results

    Here's some commented code:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form5_Load()
    4.     LoadTheGrid "SELECT * FROM Asbestos"
    5. End Sub
    6.  
    7. Private Function OpenConnection() As ADODB.Connection
    8.     'create and open the connection
    9.     Set cn = New ADODB.Connection
    10.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Documents and Settings\Harry Scott\My Documents\Homework\Computing\Asbestos Register\Database.mdb"
    11.     cn.CursorLocation = adUseClient
    12.     cn.Open
    13. End Function
    14.  
    15. Private Sub CloseConnection(ByRef cnn As ADODB.Connection)
    16.     If Not cnn Is Nothing Then
    17.         'if the connection is open, then close it
    18.         If cnn.State = adStateOpen Then
    19.             cnn.Close
    20.         End If
    21.         Set cnn = Nothing
    22.     End If
    23. End Sub
    24.  
    25. Private Function GetRecordset(ByRef cnn As ADODB.Connection, ByVal strSQL As String) As ADODB.Recordset
    26.     Dim rs As ADODB.Recordset
    27.    
    28.     'create and open the recordset
    29.     Set rs = New ADODB.Recordset
    30.     rs.CursorLocation = adUseClient
    31.     Set rs = cn.Execute(strSQL)
    32.     'disconnect it
    33.     Set rs.ActiveConnection = Nothing
    34.     cn.Close
    35.     Set GetRecordset = rs
    36. End Function
    37.  
    38. Private Sub DoInsert(ByRef cnn As ADODB.Connection, ByVal strSQL As String)
    39.     'execute the SQL, without building a recordset object
    40.     cnn.Execute strSQL, Options:=adExecuteNoRecords
    41. End Sub
    42.  
    43. Private Sub LoadTheGrid(ByVal strSQL As String)
    44.     Dim cn As ADODB.Connection
    45.     Dim rs As ADODB.Recordset
    46.     Dim iCol As Integer, iRow As Integer
    47.     Dim fld As ADODB.Field
    48.     Dim strInsertSQL As String
    49.    
    50.     'open a connection
    51.     Set cn = OpenConnection()
    52.    
    53.     'go get the recordset
    54.     Set rs = GetRecordset(cn, strSQL)
    55.     iRow = 0
    56.     'set the row and column count for the grid
    57.     Me.MSFlexGrid1.Rows = rs.RecordCount
    58.     Me.MSFlexGrid1.Cols = rs.Fields.Count
    59.     If rs.RecordCount = 0 Then
    60.         MsgBox ("No records meet your search query.")
    61.     End If
    62.    
    63.     'loop through the records
    64.     Do Until rs.EOF
    65.         iCol = 0
    66.         'loop through the fields
    67.         For Each fld In rs.Fields
    68.             'set the value
    69.             Me.MSFlexGrid1.TextMatrix(iRow, iCol) = fld.Value
    70.             iCol = iCol + 1
    71.         Next fld
    72.         iRow = iRow + 1
    73.        
    74.         'construct the INSERT string...
    75.         strInsertSQL = "INSERT INTO [Asbestos Temp] ([Sample Number], [Unit Number], [Product Type], [Asbestos Type], [Location In Unit], [PBC Job Number], [Work Done], [Date Of Completion], [Air Test Certificate Number], [Assessment Score - Material], [Assessment Score - Priority], [Assessment Score - Total]) VALUES (""" & rs.Fields("Sample Number") & """,""" & rs.Fields("Unit Number") & """,""" & rs.Fields("Product Type") & """,""" & rs.Fields("Asbestos Type") & """,""" & rs.Fields("Location In Unit") & """,""" & rs.Fields("PBC Job Number") & """,""" & rs.Fields("Work Done") & """,""" & rs.Fields("Date Of Completion") & """,""" & rs.Fields("Air Test Certificate Number") & """,""" & rs.Fields("Assessment Score - Material") & """,""" & rs.Fields("Assessment Score - Priority") & """,""" & rs.Fields("Assessment Score - Total") & """)"
    76.         '...and fire it
    77.         DoInsert cn, strInsertSQL
    78.        
    79.         'get the next record
    80.         rs.MoveNext
    81.     Loop
    82.    
    83.     'tidy up
    84.     rs.Close
    85.     Set rs = Nothing
    86.     CloseConnection cn
    87. End Sub
    88.  
    89. Private Sub CmdBack_Click()
    90.     frmEditAsbestos.Show
    91.     Form5.Hide
    92. End Sub
    93.  
    94. Private Sub CmdSearch_Click()
    95.     Dim strSQL As String, strWhere As String
    96.    
    97.     'the basic SQL string
    98.     strSQL = "SELECT * FROM Asbestos"
    99.    
    100.     'now build the WHERE clause
    101.     If Text1.Text <> vbNullString Then
    102.         strWhere = strWhere & " AND [Sample Number] LIKE ""%" & Text1.Text & "%"""
    103.     End If
    104.     If Text2.Text <> vbNullString Then
    105.         strWhere = strWhere & " AND [Unit Number] LIKE ""%" & Text2.Text & "%"""
    106.     End If
    107.     If Text3.Text <> vbNullString Then
    108.         strWhere = strWhere & " AND [Product Type] LIKE ""%" & Text3.Text & "%"""
    109.     End If
    110.     If Text4.Text <> vbNullString Then
    111.         strWhere = strWhere & " AND [Asbestos Type] LIKE ""%" & Text4.Text & "%"""
    112.     End If
    113.     If Text5.Text <> vbNullString Then
    114.         strWhere = strWhere & " AND [Location In Unit] LIKE ""%" & Text5.Text & "%"""
    115.     End If
    116.     If Text6.Text <> vbNullString Then
    117.         strWhere = strWhere & " AND [PBC Job Number] LIKE ""%" & Text6.Text & "%"""
    118.     End If
    119.     If Text7.Text <> vbNullString Then
    120.         strWhere = strWhere & " AND [Work Done] LIKE ""%" & Text7.Text & "%"""
    121.     End If
    122.     If Text8.Text <> vbNullString Then
    123.         strWhere = strWhere & " AND [Date Of Completion] LIKE ""%" & Text8.Text & "%"""
    124.     End If
    125.     If Text9.Text <> vbNullString Then
    126.         strWhere = strWhere & " AND [Air Test Certificate Number] LIKE ""%" & Text9.Text & "%"""
    127.     End If
    128.     If Text10.Text <> vbNullString Then
    129.         strWhere = strWhere & " AND [Assessment Score - Material] LIKE ""%" & Text10.Text & "%"""
    130.     End If
    131.     If Text11.Text <> vbNullString Then
    132.         strWhere = strWhere & " AND [Assessment Score - Priority] LIKE ""%" & Text11.Text & "%"""
    133.     End If
    134.     If Text12.Text <> vbNullString Then
    135.         strWhere = strWhere & " AND [Assessment Score - Total] LIKE ""%" & Text12.Text & "%"""
    136.     End If
    137.     If strWhere <> vbNullString Then
    138.         strSQL = strSQL & " WHERE " & Mid$(strWhere, 5)
    139.     End If
    140.    
    141.     'fill the grid
    142.     LoadTheGrid (strSQL)
    143. End Sub
    144.  
    145. Private Sub Form5_Unload(Cancel As Integer)
    146. '
    147. End Sub
    I've rejigged it a bit as well - just my personal taste.

    What I was trying to explain earlier was that when you are constructing your INSERT string, rsSearch wasn't 'visible' to your code. By moving it into the LoadTheGrid method, you can fire the INSERT for each row you return for display in the grid.

    [BTW I agree with szalamany, this does seem rather a lot of work for little gain - there are several other ways to achieve this result.]

  23. #23
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a report from search results

    HScott - look at this other thread from today...

    http://www.vbforums.com/showthread.php?t=334138

    Why can't you print like this - and be just as happy (even happier) than you are with the method you are trying to make work??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  24. #24

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    40

    Re: Generate a report from search results

    The results are now added to the Asbestos Temp table when I click search.
    How can I delete the contents of the table when I click the back button?
    Also, how can I open a report from access in VB?

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

    Re: Generate a report from search results

    Aha! Now you see why szalamany was suggesting you print with the Printer object. Opening Access reports from within VB can get a bit ugly. There are plenty of threads on these boards that discuss it, e.g. recently this one
    (This is the search I did.)

    Delete SQL is pretty straightforward:
    Code:
    DELETE From [Asbestos Temp]
    DELETE From [Asbestos Temp] WHERE criteriaString
    (Glad the INSERT is working though )

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