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, "
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?
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, "
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.