views:

14

answers:

1

I am using this code for importing encrypted excel into access 2007:

xlApp.Workbooks.Open FileName:=dlgOpen.SelectedItems(i), PASSWORD:=strPasswd

It works nicely if I give the correct password, or nothing (I'll prompt user to enter the password again for they give a blank password), but not for wrong password, Access will prompt for error "1004", saying that the password is not correct, and ask for debug.

How can I change it so that it will reject user's entry and prompt for password again?

Thanks!

+2  A: 

You would have to do some error trapping. Off the top of my head something like this

Public Sub Import (strPassword as String)
On error goto Error_trap
If len(strPassword)=0 then
    strPassword=InputBox(“Enter Password”)
End if
xlApp.Workbooks.Open FileName:=dlgOpen.SelectedItems(i), PASSWORD:=strPasswd

Exit Sub

Error_trap:

If err.Number=1004 then
    If msgbox(“No Password entered, do you want to try again?”,36)=vbYes then
        Import ""
    Else
        Msgbox “Ok fine be that way”
    End if
Else
    Msgbox “Error happened in sub Import, error description, “ & err.description
End if
End Sub
Kevin Ross
Just a code note: it's easiest to use a CASE SELECT for branching your error handler, since even though you start with only two possibilities, you might later need another. Just today I was coding an error handler and started out with two branches, and in testing, encountered a 3rd that needed handling. If I'd used an If/Then/Else it would have been complicated, but since I was using CASE SELECT I just added the new case...
David-W-Fenton