Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to avoid opening a password-protected mdb Expand / Collapse
Author
Message
Posted Friday, October 25, 2013 3:02 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
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
Post #1508369
Posted Friday, October 25, 2013 5:21 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 2,397, Visits: 2,935
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’! **
Post #1508407
Posted Friday, October 25, 2013 5:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
Thanks, what's the code to do that? I'm looping through all the folders on my pc's hard drive.
Post #1508414
Posted Friday, October 25, 2013 6:08 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 2,397, Visits: 2,935
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’! **
Post #1508423
Posted Friday, October 25, 2013 10:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
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.
Post #1508521
Posted Monday, October 28, 2013 2:12 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
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

Post #1509109
Posted Tuesday, October 29, 2013 2:09 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 2,397, Visits: 2,935
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’! **
Post #1509561
Posted Tuesday, October 29, 2013 2:48 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
Thank you, if you look at
app.OpenCurrentDatabase strPath, , ""
you can see the password argument is indeed filled, just by a zero-length password
Post #1509573
Posted Wednesday, October 30, 2013 4:09 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 2,397, Visits: 2,935
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’! **
Post #1509973
Posted Thursday, October 31, 2013 4:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:43 AM
Points: 100, Visits: 795
You're right, I get no error. How do I query for a list of tables within the objConnection object?
Post #1510093
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse