October 25, 2013 at 3:02 am
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
October 25, 2013 at 5:21 am
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.
October 25, 2013 at 5:45 am
Thanks, what's the code to do that? I'm looping through all the folders on my pc's hard drive.
October 25, 2013 at 6:08 am
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.
October 25, 2013 at 10:16 am
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.
October 28, 2013 at 2:12 pm
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
October 29, 2013 at 2:09 pm
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.
October 29, 2013 at 2:48 pm
Thank you, if you look at
app.OpenCurrentDatabase strPath, , ""
you can see the password argument is indeed filled, just by a zero-length password
October 30, 2013 at 4:09 pm
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.
October 31, 2013 at 4:19 am
You're right, I get no error. How do I query for a list of tables within the objConnection object?
October 31, 2013 at 7:57 am
Take a look at this site: http://allenbrowne.com/func-ado.html. You can use the sample to query the database for a list of tables. Hopefully this will give you an error when executing against a password protected database, so you know you'll need to skip the rest of the function.
October 31, 2013 at 10:31 am
Thank you, still no can do. Following your latest advice, the code is now
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
ShowSchema
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 Subbut it just glides through the ShowSchema function without any error when it encounters a password-protected mdb(which I've attached to this post); for your info, the content of the immediate window, after it does that is
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
tblEvents
November 2, 2013 at 12:14 pm
The code below will give you a working example. It will loop through all files within the given folder. When the file is of a type: "Microsoft Access Database" then an ADODB connection is setup to that file. When an errors occurres a message is displayed stating the filename and the error message. Else the message is displayed stating the file is accessible.
strPath = "{your folder path here}"
Set objConn = CreateObject("ADODB.Connection")
' create objects to the file system
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(strPath)
Set colItems = objFolder.Items
' ignore errors and proceed to the next line
On Error Resume Next
' loop through each file
For Each objItem in colItems
' check the file type
if objItem.type = "Microsoft Access Database" then
' build the connection string and try to open the file
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & "\" & objItem.name & ";"
objConn.Open strConnect
' check for errors
If Err.Number = 0 Then
' no error: replace the line below with the code you need to perform on an accessible database
wscript.echo objItem.name & " can be opened." & vbCrLf
else
' error occurred: remove this ELSE block if you don't want any messages regarding inaccessible databases
wscript.echo objItem.name & " is inaccessible." & vbCrLf & err.description & vbCrLf
end if
' reset error
Err.Clear
end if
Next
wscript.echo "End script"
Replace the messages (ECHO commands) with the applicable code suitible to your needs.
November 3, 2013 at 3:28 am
Hi again, thank you, I'll only be able to test your code tomorrow on a password-protected mdb. I hope I'm wrong but don't you think that since your latest code mimics your previous code:
strConnection = GetConnectionstring(strPath)
objConnection.Open strConnection
no error will be raised even when a password-protected mdb is encountered?
November 3, 2013 at 1:25 pm
Yesterday I was finally able to create a test case of my own. The code I provided in the previous post is tested by me.
Initially I just used the code "objConn.Open strConnect". This did indeed default pop-up with the password-mismatch message. But when I also used the code "On Error Resume Next" it didn't pop-up anymore and the process continued to the "if err.number" block, so I could display my own message box.
Please let me know your results...
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply