title: How to create an Access report from a query using vb.net
body: <p>Let me explain what trying to do. I have a vb.net form linked with an access database. The form let you make a query and search the database. Now I want to put the option to print a report from the same query.</p>
  
  <p>This it what my form look like: </p>
  
  <p><img src="https://i.stack.imgur.com/GQhbI.png" alt="enter image description here"></p>
  
  <ol>
  <li>I want to let the user chose what he want to see in the report</li>
  <li>Create a report from the query </li>
  <li>Be able to preview the report</li>
  <li><p>Print it</p>
  
  <p>I could not found anywhere how to create report using a specific query.</p></li>
  </ol>
  
  <p><strong>What I was able to do</strong>:</p>
  
  <ol>
  <li>I was able to print a report that has already been created in access using this <a href="http://support.microsoft.com/en-us/kb/317113/en-us?p=1" rel="nofollow noreferrer">link</a>.</li>
  <li>I was able to print show the results of the query in an excel sheet.</li>
  </ol>
  
  <hr>
  
  <p>This is the part of my code where I connect to the database and show the results in excel</p>
  
  <pre><code>    ' Connect to the database and send the query
      Dim con As New OleDb.OleDbConnection
      Dim ds As New DataSet
      Dim da As OleDb.OleDbDataAdapter
      Dim MaxRows As Integer
  
      Try
          con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\docs-management.mdb"
          con.Open()
  
          da = New OleDb.OleDbDataAdapter(sql, con)
  
          da.Fill(ds, "DocList")
  
          ' Discover if there's a successful search
          MaxRows = ds.Tables("DocList").Rows.Count
  
          If MaxRows = 0 Then
              MsgBox("No documents were found using this filter.")
              con.Close()
              Exit Sub
          End If
  
          Dim YesOrNoAnswerToMessageBox As String
          Dim QuestionToMessageBox As String
  
          QuestionToMessageBox = MaxRows &amp; " Document(s) have been found and will be put into an excel spreadsheet." &amp; _
          vbCrLf &amp; "Would you like to continue?"
  
          YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "Narrowing your search")
  
          If YesOrNoAnswerToMessageBox = vbNo Then
              Exit Sub
          Else
          End If
  
          Dim oExcel As Object
          Dim oBook As Object
          Dim oSheet As Object
          oExcel = CreateObject("Excel.Application")
          oExcel.Visible = True
          oBook = oExcel.Workbooks.Add
          oSheet = oBook.Worksheets(1)
  
  
  
          'Transfer the data to Excel
          For columns = 0 To ds.Tables("DocList").Columns.Count - 1
              oSheet.Cells(1, columns + 1) = ds.Tables("DocList").Columns(columns).ColumnName
          Next
          oSheet.Rows("1:1").Font.Bold = True
          For col = 0 To ds.Tables("DocList").Columns.Count - 1
              For row = 0 To ds.Tables("DocList").Rows.Count - 1
                  oSheet.Cells(row + 2, col + 1) = ds.Tables("DocList").Rows(row).ItemArray(col)
                  ' This is where we make hyperlinks out of the file locations
                  If ds.Tables("DocList").Columns(col).ToString = "File_Location" Then
                      oSheet.Hyperlinks.Add(Anchor:=oSheet.Cells(row + 2, col + 1), Address:=ds.Tables("DocList").Rows(row).ItemArray(col), TextToDisplay:=ds.Tables("DocList").Rows(row).ItemArray(col))
                  End If
              Next
          Next
  
          con.Close()
  
      Catch
          MsgBox("An error has been generated while contacting or transfering data from the database.")
      End Try
  </code></pre>