Results 1 to 30 of 30

Thread: Access report with a twist

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Resolved Access report with a twist

    Hi all, hope someone can be of help. have successfully created routine to open an existing report, set it's record source to an SQL statement and print it out. This is done in VBA through an Access form. What i would like to do is open the report from a VB6 front end, set the record source to an SQL statement, print it out, save the report under a new name leaving the original template untouched. of course all this will occur without Access being visible to the user!This would be useful for archival purposes on a system I'm putting together. My code for the original approach is below:

    VB Code:
    1. Private Sub cmdReport_Click()
    2. Dim strSQL As String
    3. strSQL = "Select tblTransaction.UserID, tblTransaction.TransactionNumber,tblDetails.Quantity, tblProduct.SellPrice, tblProduct.Description, (tblDetails.Quantity * tblProduct.SellPrice) As Total  From tblTransaction, tblDetails, tblProduct" & _
    4. " Where Month(Now) = Month (tblTransaction.SellDate)And tblTransaction.TransactionNumber = tblDetails.TransactionNumber And tblDetails.ProductID=tblProduct.ProductID Group By tblTransaction.UserID, tblTransaction.TransactionNumber, tblProduct.Description, tblDetails.Quantity, tblProduct.SellPrice;"
    5. DoCmd.Echo False
    6. DoCmd.OpenReport "rptTemplate", acViewDesign
    7. Reports("rptTemplate").RecordSource = strSQL
    8. DoCmd.Close , , acSaveYes
    9. DoCmd.Echo True
    10. DoCmd.OpenReport "rptTemplate", acViewNormal
    11. DoCmd.Close , "rptTemplate", acSaveYes
    12. End Sub

    Could some one point me in the right direction as to how to do this please?

    Thanks, Lol
    Last edited by Lol Owen; Apr 12th, 2005 at 04:48 PM.

  2. #2
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Access report with a twist

    I don't have the answer, but you could probably search on terms such as-
    automating access reports

    What I am pretty sure I have read is, all the users will have to have Office Pro (or at least Access) installed on their machines.
    Is that going to be a problem ?
    Rob C

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, not a problem re: Access on the machine. It's going to be a 1 pc system! Basically it'ss my industry project for a Business IT course I'm on. None of the lecturers know this so I'm in a tight spot! I've found a number of posts where a report is opened and printed from Vb but none where an existing report is printed and then re-named, hence the new thread. Because there's no SaveAs method in the DoCmd object I can't use that. One way, if possible, would be to create a new object "equal" to the existing report and re-name that. I just don't know how to assign all the properties, for want of a better word, of the existing report to a new Access report object. Then this new object could be stored using a SaveAs command.Any thoughts?

    Lol

  4. #4
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Access report with a twist

    I'm not much help I fear.
    It is only recently (out of necessity), that I managed to automate excel, to mirror my SGrid2's contents into Excel,
    The User mentioned that Excel had neat printing abilities, and I knew that trying to print a grid that was wider than texas, was going to be a major task.
    So automating Excel was a life saver.

    I note that this site has a VBA forum. Would posting there be more fruitful ?
    Rob C

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Thanks for your input Rob, if I source the solution elsewhere I'll post it up here for othe users.

    Lol

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    There are possibilities for what you want to do. You could just create the report on the fly dynamically.
    Then the "template" will be in your vb code.

    This is my code example on how to create an Access report dynamically from VB6.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, I've looked at creating the report on the fly, only trouble being I couldn't figure out how to put text boxes for Sum functions into say report footer. Every time I tried to enter a position for the text box in the create string I got an error saying it was an invalid value. Plus it's easier to make really nice reports in Access. Any further thoughts?

    Cheers, Lol

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    I always seem to do things the hard way

    Try this out in VB6 by adding a reference to MS Access xx.0 Object Library

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim oApp As Access.Application
    3.     oApp.Visible = True
    4.     oApp.DoCmd.OpenReport "Report1", acViewPreview, , , acDialog
    5.     DoCmd.CopyObject , "Report1a", acReport, "Report1"
    6.     Dim oReport As Access.Report
    7.     Set oReport = oApp.Reports("Report1a")
    8.     oReport.Print
    9. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Thanks Rob, that looks to do exactly what I was trying to explain. i suppose the string to save it would be along the lines of:

    oreport.DoCmd.SaveReport "C:\Lol\Report1a"

    I'm self taught at most of this stuff as my college lecturers can't do most of it If I've got the save string wrong feel free to correct me

    Many thanks for your help, Lol

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    the oReport object does not contain a docmd object.
    You would have to use the application's docmd to save the currently opened report.

    VB Code:
    1. oApp.DoCmd.Save acReport, "Report1a"
    This would save the current version of the report to the db, not the filesystem.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Thanks again Rob, am on a steep learning curve on this project, I really apprecciate the help.

    Cheers, Lol

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Not a problem.

    I noticed that you seemed to be asking how to save the report to a file?
    Is this something else you need to do?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    On a side note, say i wanted to save the report as say "MonthEnd"&Format(Now,"Medium Date") how would I do this? Would it be along the lines of

    oApp.DoCmd.save acReport "'MonthEnd' & Format(Now,"Medium Date")" or would the ' delimiters be superfluous, or would the extra "" cause problems?
    Reason I ask is these things will be saved as "Month End" and then the date appendage, either the current date formatted or a range of dates selected by the user as part of the original SQL string for the record source?

    Lol

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    VB Code:
    1. DoCmd.CopyObject , "MonthEnd_" & Format(Now,"mm-dd-yyyy"), acReport, "Report1"
    The date delimiters will cause an issue so its better to do it this way. There are certain characters that Access
    doesnt like for names of its objects and different characters that will messup your sql statement too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Thanks Rob, that makes sense, will give it a try and post the results. Many thanks for your help! As I'm doing this from a VB6 front end and connecting to an Access dbase I assume I just initiate a connection in the standard way, do my report code, then close the connection?

    Lol

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, tried your code thanks but it sprang a few errors. here is the code I am using

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim cnABOF As ADODB.Connection ' set connection
    3. Dim strConnection As String ' declare connection string
    4.  
    5.    
    6.   strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    7.                 "Data Source=" & "C:\Documents and Settings\Lol\Desktop\Project Final 9th April\Database\French.mdb;" ' text for connection
    8. Set cnABOF = New ADODB.Connection ' set new ADODB connection
    9. cnABOF.Open strConnection ' open connection
    10.    
    11.    
    12.    Dim oApp As Access.Application
    13.     Set oApp = New Access.Application ' added this also
    14.     oApp.Visible = True
    15.     oApp.DoCmd.OpenReport (rptTemplate), acViewNormal ' my code for opening "cannot perform this action at this time
    16.    
    17.  
    18.     DoCmd.CopyObject , "Report1a", acReport, "rptTemplate"
    19.     Dim oReport As Access.Report
    20.     Set oReport = oApp.Reports("Report1a")
    21.     oReport.Print
    22. oApp.DoCmd.Save acReport, "Report1a"
    23.  
    24.  
    25. Set cnABOF = Nothing ' close connection
    26.  
    27. End Sub

    I changed the code where you open the esisting report as I got a message saying too many arguments specified. But instead, even using the intellisense provided syntax i keep getting "cannot perform that action at this time" errors. I know the connection string is correct because I can run another form in my project with it to access the same db. Any thoughts please as to what's happening?

    Cheers, Lol

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    rptTemplate should not be wrapped with parenthesis.
    Also, why connect to the db using ADO when your not using it?

    VB Code:
    1. oApp.DoCmd.OpenReport "rptTemplate", acViewPreview
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    I thought I needed to be connected to the database from VB6 or how else would it know where to get the report from?

    Lol

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, just tried the re-done open command but still get the "cannot perform the operation at this time" message.

    Lol

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Ah! I missed that. I thought we were doing this from within the access db. I think I messed up

    From VB6 we need to open the db using the Access Object Model like initially we were doing?

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim oApp As Access.Application
    3.     oApp.Visible = True
    4.     oApp.OpenCurrentDatabase "C:\MyDB.mdb", False, "YourPassword"
    5.     oApp.DoCmd.OpenReport "Report1", acViewPreview, , , acDialog
    6.     'oApp.DoCmd.CopyObject , "Report1a", acReport, "Report1"
    7.     Dim oReport As Access.Report
    8.     Set oReport = oApp.Reports("Report1a")
    9.     oReport.Print
    10.     oApp.DoCmd.CopyObject , "MonthEnd_" & Format(Now,"mm-dd-yyyy"), acReport, "Report1"
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, just tried the code, added a line and ammended another (VB6 not happy), here it is below:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim oApp As Access.Application
    3. Set oApp = New Access.Application 'added this line
    4.     oApp.Visible = True
    5.     oApp.OpenCurrentDatabase " C:\Documents and Settings\Lol\Desktop\Project Final 9th April\Database\French.mdb", False' removed password statement
    6.  
    7.    
    8.     oApp.DoCmd.OpenReport "rptTemplate", acViewPreview ' changed this because of arguments
    9.     oApp.DoCmd.CopyObject , "Report1a", acReport, "rptTemplate"
    10.     Dim oReport As Access.Report
    11.     Set oReport = oApp.Reports("Report1a")
    12.     oReport.Print
    13.     oApp.DoCmd.CopyObject , "MonthEnd_" & Format(Now, "mm-dd-yyyy"), acReport, "Report1"
    14.  
    15. End Sub

    I'm now getting runtime error 7866 "cannot open database because missing or opened exclusaively by another user".

    I really appreciate your help on this and would like to crack it but if it's becoming a chore just say and we can forget it.

    Lol

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Check the directory where the db is located for a .ldb file. Usually VB will retain a connection under ADO to the db
    and keep it locked. If so then close vb and re-open it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    No ldb file showing. Closed Vb and re-opened satill not having it. Connection path correct and ADO is closing the connection properly because I just checked for an ldb file after running my form with the ADO in it. Curious!

    Lol

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Ok, if your connecting to the db using ADO and using the Acess Object Model to open the report then your connecting
    to it twice, once with each technology.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    The form with the ADO connection on it only runs the connection on a click event on various buttons. There's no way Access could be picking up a reference to it from an uninitiated ADO connection and see this as a link, thus denying the Access Object connection, is there?

    Lol

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    I dont think so as long as your closing the connection and destroying the objects inside each button
    click. This would ensure that the ADO is not in conflict. Or as a test you could comment out the ADO code
    and see if it works then.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, tried taking a copy of the database to a new folder, creating new project and running the code but still get the "cannot open database etc".

    Looks like a doozy mate. Not a problem and I appreciate your efforts. if you want to walk away from this one I can't fault you! if wev'e hit the end of the road could you do me one last huge favour? please could you configure the code toi sit behind an Access form and I'll just run a form with no ingress to the database for the user. When I say code I'm referring to the copying object and re-naming, saving etc.

    Cheers, Lol

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Your db is ok and not corrupted? Can you open it from a double-click?

    The Access code for behind an Access Form's Button would be like so. I made some changes so it seems to
    work correctly now.

    VB Code:
    1. Private Sub Test2()
    2.  
    3.     Dim sStamp As String
    4.     Dim oApp As Access.Application
    5.     Set oApp = GetObject(, "Access.Application") 'Setting oApp as a New access.applicaion was
    6.     'creating the 2nd instance and preventing the access to the db.
    7.     'oApp.OpenCurrentDatabase "C:\MyDB.mdb", False, "YourPassword" 'If Report1 is not in the current db
    8.     sStamp = "MonthEnd_" & Format(Now, "mm-dd-yyyy")
    9.     oApp.DoCmd.CopyObject , sStamp, acReport, "Report1"
    10.     oApp.DoCmd.OpenReport sStamp, acViewPreview, , , acWindowNormal
    11.     Dim oReport As Access.Report
    12.     Set oReport = oApp.Reports(sStamp)
    13.     'oReport.Print
    14.     oApp.DoCmd.Close acReport, sStamp, acSaveYes
    15.     Set oReport = Nothing
    16.    
    17. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Access report with a twist

    Hi Rob, no my db is not corrupted and works fine. tried the code and it works great thanks! By your inference then if Setting the oApp as a new Access application was creating a second connection then changing the Set to Get in the VB code should work as well? Definitely gives me something to play with!

    Thanks, Lol

  30. #30
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access report with a twist

    Thats correct. GetObject only works if Access is already running.

    Later
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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