[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Printing an access report in a passworded DB

Posted on 2004-11-08
Medium Priority
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
  • 3
  • 3
LVL 33

Expert Comment

ID: 12530221
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

Author Comment

ID: 12530326
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!

Expert Comment

ID: 12530333
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 12530416

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

sajuks earned 2000 total points
ID: 12530462
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
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20113787.html for different options

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

Author Comment

ID: 12550922
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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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