SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to avoid opening a password-protected mdb


How to avoid opening a password-protected mdb

Author
Message
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
I'm looping through my pc's folders to open each mdb that I encounter and look for a sub in its vba modules. The problem is there are a few mdb's which have been password-protected and so I'd like to skip opening them since I don't know the password so I've tried to solve it as you see below because when the dialog box asking for the database password comes up and I click the Cancel button I get the message: "run-time error '2467': The expression you entered refers to an object that is closed or doesn't exist." but it doesn't allow me to exit the dialog box gracefully and continue the scan with the next mdb so I commented the code out in the sub below; what can I do to achieve that?

Sub ProcessDatabase(ByVal strPath As String)
Dim vbc As VBIDE.VBComponent
On Error GoTo ExitHere
app.OpenCurrentDatabase strPath, , ""
For Each vbc In app.VBE.ActiveVBProject.VBComponents
ProcessModule vbc.CodeModule, strPath
Next vbc
ExitHere:
On Error Resume Next
' If Err = 2467 Then
' app.CloseCurrentDatabase
' Exit Sub
' End If
app.CloseCurrentDatabase
End Sub

Giorgio
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4420 Visits: 3670
Before opening the database with "app.OpenCurrentDatabase" you could first try to connect with an OLEDB connection. The OLEDB connection doesn't use a GUI so you can easily handle an error in the code. If the connection succeeds you can open the database, if the connection fails you should skip it.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
Thanks, what's the code to do that? I'm looping through all the folders on my pc's hard drive.
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4420 Visits: 3670
I'm not sure if this code is compatible with VBA, but I use this (modified for security reasons) code below in VB6:
 
Private Function GetConnectionstring(ByVal strDatabase As String) As String

Dim strProvider As String

On Error GoTo ErrorHandler

strProvider = "Microsoft.Jet.OLEDB.4.0"
GetConnectionstring = "Provider=" & strProvider & ";Data Source=" & strDatabase & ";Jet OLEDB"

End Function


Dim strDatabase As String
Set objConnection = New ADODB.Connection
Set strDatabase = "{full_path_to_file}"
strConnection = GetConnectionstring(strDatabase)
objConnection.open strConnection



If you can't use this code I'm sure you can find many samples on the internet. Just search for "OLEDB VBA Access" or something similar.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
Thank you so much, it definitely runs without a hitch but I'll be able to test it against password-protected mdb's only on Monday so I'll let you know then.
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
Unfortunately, even by using the code you see below, whenever the loop encounters a password-protected mdb, the dialog box asking for the password still comes up, any other workarounds?

Private Function GetConnectionstring(ByVal strDatabase As String) As String

Dim strProvider As String

On Error Resume Next

strProvider = "Microsoft.Jet.OLEDB.4.0"
GetConnectionstring = "Provider=" & strProvider & ";Data Source=" & strDatabase & ";Jet OLEDB"

End Function


Sub ProcessDatabase(ByVal strPath As String)
Dim vbc As VBIDE.VBComponent, objConnection As ADODB.Connection, strConnection As String

On Error GoTo ExitHere
Set objConnection = New ADODB.Connection
strConnection = GetConnectionstring(strPath)
objConnection.Open strConnection
objConnection.Close
app.OpenCurrentDatabase strPath, , ""
For Each vbc In app.VBE.ActiveVBProject.VBComponents
ProcessModule vbc.CodeModule, strPath
Next vbc
ExitHere:
On Error Resume Next
' If Err = 2467 Then
' app.CloseCurrentDatabase
' Exit Sub
' End If
app.CloseCurrentDatabase
End Sub


HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4420 Visits: 3670
If the previous doesn't work I'll have to guess some follow up steps.
What happens if you supply a dummy password? Will this generate an error or will it still pop-up a dialog box? If I recall correct you can always add a password to the connection string. In case of a non-password protected database the password will be ignored.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
Thank you, if you look at
app.OpenCurrentDatabase strPath, , ""
you can see the password argument is indeed filled, just by a zero-length password
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4420 Visits: 3670
Unfortunate I'm currently not able to setup or test this myself. I can only give you some pointers...

So you say you don't get an error with the "objConnection.Open strConnection" command when you execute this against a password protected database? And what if you query for example for a list of tables within this "objConnection" object? Will the result of that give you something to conclude if you are querying against a password protected database?
You need to do something within the ADODB object to see if you are accessing a password protected database or not. After that you only proceed to open the database with "OpenCurrentDatabase" if it is not password protected.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
grovelli-262555
grovelli-262555
SSC Veteran
SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)SSC Veteran (255 reputation)

Group: General Forum Members
Points: 255 Visits: 885
You're right, I get no error. How do I query for a list of tables within the objConnection object?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search