detect password protected mdb's

J

J. Freed

I have a program that reads the contents of MSysObjects from different mdb's
and copies them into another mdb. Since the program will be reading several
thousand mdb's it has to run unattended into the night. Problem: when it hits
a password-protected file it stops to wait for someone to type in a
password/cancel. Is there a way to programmatically detect password-protected
files without actually opening them? TIA.......
 
D

Dirk Goldgar

(re-sending because my original reply hasn't appeared)

J. Freed said:
I have a program that reads the contents of MSysObjects from different
mdb's
and copies them into another mdb. Since the program will be reading
several
thousand mdb's it has to run unattended into the night. Problem: when it
hits
a password-protected file it stops to wait for someone to type in a
password/cancel. Is there a way to programmatically detect
password-protected
files without actually opening them? TIA.......


You could attempt to open each database in code and trap the error if you
can't. For example,

Dim db As DAO.Database
Dim strDBPath As String

On Error Resume Next

' Demo test of unsecured DB.
strDBPath = "C:\Users\Dirk\Documents\Unsecured.mdb"
Err.Clear
Set db = Application.DBEngine.OpenDatabase(strDBPath, , True)
If Err.Number <> 0 Then
Debug.Print "Can't open '" & strDBPath & _
"'. Error was " & Err.Number & ": " & Err.Description
Else
' Opened okay, so close it.
Debug.Print "Opened '" & strDBPath & "'"
db.Close
End If

' Demo test of DB with password.
strDBPath = "C:\Users\Dirk\Documents\Passworded.mdb"
Err.Clear
Set db = Application.DBEngine.OpenDatabase(strDBPath, , True)
If Err.Number <> 0 Then
Debug.Print "Can't open '" & strDBPath & _
"'. Error was " & Err.Number & ": " & Err.Description
Else
' Opened okay.
Debug.Print "Opened '" & strDBPath & "'"
db.Close
End If

' Demo test of DB with User-Level Security.
strDBPath = "C:\Users\Dirk\Documents\ULS_Secured.mdb"
Err.Clear
Set db = Application.DBEngine.OpenDatabase(strDBPath, , True)
If Err.Number <> 0 Then
Debug.Print "Can't open '" & strDBPath & _
"'. Error was " & Err.Number & ": " & Err.Description
Else
' Opened okay.
Debug.Print "Opened '" & strDBPath & "'"
db.Close
End If

The above code can be expected to display the following lines in the
Immediate window:

Opened 'C:\Users\Dirk\Documents\Unsecured.mdb'
Can't open 'C:\Users\Dirk\Documents\Passworded.mdb'. Error was 3031:
Not a valid password.
Can't open 'C:\Users\Dirk\Documents\ULS_Secured.mdb'. Error was 3033:
You do not have the necessary permissions to use the
''C:\Users\Dirk\Documents\ULS_Secured.mdb' object. Have your system
administrator or the person who created this object establish the
appropriate permissions for you..
 
C

Chris O''Neill

Dirk Goldgar said:
You could attempt to open each database in code and trap the error if you
can't. For example,

[SNIP!]

This is a related question...

I'm trying to open another database and edit a field in a table. The table
(tblLicense) has been set using ULS to ReadDesign/ReadData/UpdateData
permissions for a special username that I'm using in my code. My code goes
something like this (after all the Dim statements):

'********* Start Code Snippet ***********
Set db = OpenDatabase("c:\Program Files\Balloon Business Management
System\balloons_secured_DATA_DEMO.mdb", , ,
"UID=Username_Here;PWD=Password_Here")
Set rs = db.OpenRecordset("tblLicense", dbOpenDynaset)

With rs
Debug.Print !AppVersion
.Edit
!AppVersion = "1.01"
.Update
Debug.Print !AppVersion
End With
Set rs = Nothing
Set db = Nothing
'********* End Code Snippet ***********

The code runs fine until it gets to the ".Edit" line and then bombs with a
3033 error message, even though it appears to be opening the database with
the right username and password.

What am I doing wrong??? As always, any help and suggestions provided are
greatly appreciated!

Regards, Chris
 
D

Dirk Goldgar

Chris O''Neill said:
Dirk Goldgar said:
You could attempt to open each database in code and trap the error if you
can't. For example,

[SNIP!]

This is a related question...

I'm trying to open another database and edit a field in a table. The
table
(tblLicense) has been set using ULS to ReadDesign/ReadData/UpdateData
permissions for a special username that I'm using in my code. My code
goes
something like this (after all the Dim statements):

'********* Start Code Snippet ***********
Set db = OpenDatabase("c:\Program Files\Balloon Business Management
System\balloons_secured_DATA_DEMO.mdb", , ,
"UID=Username_Here;PWD=Password_Here")
Set rs = db.OpenRecordset("tblLicense", dbOpenDynaset)

With rs
Debug.Print !AppVersion
.Edit
!AppVersion = "1.01"
.Update
Debug.Print !AppVersion
End With
Set rs = Nothing
Set db = Nothing
'********* End Code Snippet ***********

The code runs fine until it gets to the ".Edit" line and then bombs with a
3033 error message, even though it appears to be opening the database with
the right username and password.

What am I doing wrong??? As always, any help and suggestions provided are
greatly appreciated!


Is the database in which you're running this code using the same workgroup
file as the secured database you're trying to open? My guess is not, though
it's possible they're both using system.mdw. In general, to open a database
that has been secured with user-level security, you have to use the DBEngine
or PrivDBEngine object to specify the workgroup file (SystemDB), user, and
password. Something like this:

'----- start of code -----

Dim dbe As New PrivDBEngine
Dim db As DAO.Database

Dim strFolder As String
Dim strDBName As String
Dim strWorkgroup As String

strFolder = C:\Program Files\Balloon Business Management System\"
strDBName = "balloons_secured_DATA_DEMO.mdb"
strWorkgroup = "balloons.mdw"

With dbe
.SystemDB = strFolder & strWorkgroup
.DefaultUser = "Username_Here"
.DefaultPassword = "Password_Here"
Set db = .OpenDatabase(strFolder & strDBName)
End With

Set rs = db.OpenRecordset("tblLicense", dbOpenDynaset)
With rs
Debug.Print !AppVersion
.Edit
!AppVersion = "1.01"
.Update
Debug.Print !AppVersion
.Close
End With
Set rs = Nothing

db.Close
Set db = Nothing
Set dbe = Nothing

'----- end of code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top