Results 1 to 6 of 6

Thread: Access Report

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Access Report

    I am generating a report in Access from a query. How can I modify the Query from VB?
    I am not curently sending and SQL query, i am using one generated in Access. The problem is i want to be able to change the query (or pass a query) at runtime without giving the user direct access to the database. I am currently using:
    Code:
    Private Sub button_Click()
    Dim strDummy As String
    
    On Error Resume Next
    
    strDummy = Printer.DeviceName
       
    If Err.Number Then
        MsgBox "There are no printers installed on this computer. Please install a printer and try again.", vbInformation
    Else
        RunAccessReport App.Path & "\RecuitmentPlanner.mdb", "Candidates"
    End If
    End Sub
    
    Public Sub RunAccessReport(strDB As String, strReport As String)
    Dim AccessDB As Access.Application
    
        Set AccessDB = New Access.Application
        AccessDB.OpenCurrentDatabase strDB
        AccessDB.DoCmd.OpenReport strReport, acViewPreview
        AccessDB.Visible = False
        AccessDB.DoCmd.PrintOut (acPrintAll)
        AccessDB.Quit acQuitSaveAll
        Set AccessDB = Nothing
        
    End Sub
    How can i pass a query using this method or is there another way to generate reports?

  2. #2
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    I have worked out a solution for you.
    It may seem a bit complicated, but i think this is the only way to do it.

    In your access DB, create 2 querys, qryBase and qryReport.
    The 2 querys must be identique, lets say the sql for the query's is like this: SELECT Table1.Field1
    FROM Table1
    WHERE (((Table1.Field1)="ToBeReplaced"));
    Build a report that uses qryReport as source.
    The function i wrote will open access, copy the sql from qryBase to qryReport, and replace the criteria 'TobeReplaced' by 'aa'.
    Then it opens the report, still based on query qryReport.

    The use of the query qryBase is neccesary baecause otherwise you lose the searchstring 'ToBeReplaced' that you need to add your custom selection criteria.

    You can modify the function to replace any part of the sql string of qryReport.

    Let me know if this helped you out or not.

    Greetz,

    Luc

    Code:
    Public Sub RunAccessReport(strDB As String, strReport As String)
      
      Dim AccessDB As Access.Application
      Dim sSql As String
      Dim q
      Set AccessDB = New Access.Application
      AccessDB.OpenCurrentDatabase strDB
      Set q = AccessDB.CurrentDb.QueryDefs("qryReport")
      sSql = AccessDB.CurrentDb.QueryDefs("qryBase").SQL
      sSql = Replace(sSql, "ToBeReplaced", "aa")
      q.SQL = sSql
        
      AccessDB.DoCmd.OpenReport strReport, acViewPreview
      AccessDB.Visible = False
      AccessDB.DoCmd.PrintOut (acPrintAll)
      AccessDB.Quit acQuitSaveAll
      Set AccessDB = Nothing
        
    End Sub

  3. #3
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    You should read about Snapshots. That's what I use in my app so that I can use a report made in Access without letting the user even knowing you are using an Access Report. It has all the functions an Access Report has for the users, but it won't access the Access database. Pretty useful if the user doesn't have an Access license as well!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    Cheers, just what i needed. Added a few more ToBeReplace fields and it works perfectly.

    I will have a look at the SnapShot link.

  5. #5
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Actually, I think that link sucks.
    Check out this link instead. It gives you an example in VB about using the Snapshot viewer.

    Also check out this thread where I got some help about using the snapshot viewer.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    I have also found that you can do the following:
    VB Code:
    1. Public Sub RunAccessReport(strDB As String, strReport As String, strQuery As String)
    2. Dim AccessDB As Access.Application
    3. Dim sSql As String
    4. Dim q
    5.  
    6. Set AccessDB = New Access.Application
    7. AccessDB.OpenCurrentDatabase strDB
    8. Set q = AccessDB.CurrentDb.QueryDefs("QryDB")
    9. sSql = "SELECT * FROM DB"
    10. q.SQL = sSql
    11.    
    12. AccessDB.DoCmd.OpenReport strReport, acViewPreview
    13. AccessDB.Visible = False
    14. AccessDB.DoCmd.PrintOut (acPrintAll)
    15. AccessDB.Quit acQuitSaveAll
    16. Set AccessDB = Nothing
    17.    
    18. End Sub

    This gives me total control of the Query i pass to the report.

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