Printing an access report in a passworded DB

Posted on 2004-11-08
Last Modified: 2010-05-02
Hi guys,

Using VB, I'm trying to print a report on an Access database that is passworded.  Im
using DAO.  I can open the database, update etc.

Just when i try to print it creates a popup and asks for the database
password. I would like for the code to provide the password for automation...

This is the code im using.

Private Sub cmdPrint_Click()
PrintAccessReport App.Path & "\Database.mdb", "Results", False End Sub

Thanks in advance.
Question by:TheDave1
    LVL 33

    Expert Comment

    Const dbPathName = "c:\Database.mdb"
    Const strPassword = "thepassword"
    Dim Acc As Object, db As Object

    Set Acc = CreateObject("Access.Application")
    Set db = Acc.Application.DBEngine.Workspaces(0).OpenDatabase(dbPathName, 0, False, ";pwd=" & strPassword)
    Acc.OpenCurrentDatabase dbPathName, False
    Set db = Nothing
    Acc.DoCmd.OpenReport "ReportName", acPreview
    Set Acc = Nothing
    LVL 1

    Author Comment

    Thanks for the quick response.

    I tried this, but nothing happens when I click on the Print button now :/

    I left ReportName alone the first time and it spat an error back, so I know that it's along the right track... It just doesn't do anything at all with the right ReportName in there.

    Also, rather than simply opening the report I would like it to either just print the report, or open/print..

    Hope this makes sense!
    LVL 3

    Expert Comment

    this isnt related to the question but its a good practice, this will ensure that your path will always be a correct path.  If your program is installed on the root of a drive it allready has \ (ex. c:\) at the end of the string, if not there is no \ (ex c:\program files\your program) at the end.

    Dim tPath As String

    If Mid(App.Path, Len(App.Path), 1) = "\" Then
        tPath = App.Path & "Database.mdb"
        tPath = App.Path & "\Database.mdb"
    End If
    LVL 1

    Author Comment


    When I said nothing happens, it actually does open an MSACCESS.EXE process that sits there and does nothing at all..

    Any ideas??
    LVL 33

    Accepted Solution

    try this
    dim strDbName
    dim objAccess
    dim objDB
    strDbName = server.MapPath("db/mydatabase.mdb")
    Set objAccess = Server.CreateObject("Access.Application")
    objAccess.Visible = False
    objAccess.OpenCurrentDatabase strDbName
    Set objDB = objAccess.CurrentDb()
    objDB.DoCmd.OpenReport  "ReportName",acViewNormal
    set objDB = nothing
    objAccess.Quit acQuitSaveNone
    Set objAccess = Nothing

    Also chec k for different options

    I will be out for a few hrs will check back afterwards.Good luck
    LVL 1

    Author Comment

    I actually ended up fixing this problem using the sendkeys function. Thanks to sajuks for trying his best, though. I have given him the points as he helped the most.

    LVL 33

    Expert Comment

    Glad you were able to get it working. Thanks for the points and grade

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now