Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to find if a database is password protected

Status
Not open for further replies.

rewdee

Programmer
Aug 17, 2001
295
US
Hi All,

Is there a way to find out if a mdb is password protected?

Maybe somewhere in the Catalog.Users or something but can't seem to get anything.

Thanks,
Rewdee
 
Rewdee, (What a great name!!)
Here is a little function I wrote that will do what you want:

Function IsPasswordProtected(sFileName As String)
Dim db As DAO.Database

On Error Resume Next

Set db = OpenDatabase(sFileName) 'attempt to open db without a password.

Select Case Err
Case 3031 'if error 3031 occurs it's password protected
IsPasswordProtected = True
Case 0
IsPasswordProtected = False 'if no error occurs we have no password
db.Close
Case Else
IsPasswordProtected = Null 'if we get another error there's a different problem.
'probably the file doesn't exist or it's the wrong version.
MsgBox "Invalid database", vbCritical + vbOKOnly, "End Select
Set db = Nothing
End Function

You will need to set a reference to DAO before you run it though.

Use it by calling:

IsPasswordProtected("c:\desktop\pass.mdb")

and you will get True, False or null.

hth

Ben ----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Thanks Ben. I was hoping though to use ADO. I'm concerned about different DAO and Access versions? For example, If this app is on a computer that has DAO 3.5 and I make a reference to DAO 3.6, it is not going to be pretty for the user. Also can this function work if I try opening a Access XP file?

Thanks,
Rewdee
 
I'm not sure how to do it in ADO, but I will look into it, but if you are not sure what level of DAO is installed you can modify it like this:

Function IsPasswordProtected(sFileName As String)
Dim objdao As Object
Dim db As Object

On Error Resume Next

'Try jet 3.6 first
Set objdao = CreateObject("DAO.DBEngine.36")

If Err.Number = 429 Then
Err.Clear
'Now try jet 3.5
Set objdao = CreateObject("DAO.DBEngine.35")
End If

If Err.Number = 429 Then
Err.Clear
'Now try jet 3.5
Set objdao = CreateObject("DAO.DBEngine")
End If

'DAO not in project leave sub
If Err.Number = 429 Then exit sub 'cant find dao library
err.clear

Set WS = objdao.Workspaces(0)

Set db = ws.OpenDatabase(sFileName) 'attempt to open db without a password.

Select Case Err
Case 3031 'if error 3031 occurs it's password protected
IsPasswordProtected = True
Case 0
IsPasswordProtected = False 'if no error occurs we have no password
db.Close
Case Else
IsPasswordProtected = Null 'if we get another error there's a different problem.
'probably the file doesn't exist or it's the wrong version.
MsgBox "Invalid database", vbCritical + vbOKOnly, "End Select
Set db = Nothing
End Function

Now you don't need to set any references at all.

The function will be able to open any file that is the sae version or earlier, eg Access 2002 will check all versions, where A97 will only check A97 and A2 files.

hth

Ben ----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top