Link to home
Start Free TrialLog in
Avatar of jfl
jfl

asked on

Open file -> Error

An error occurs if I try to open a non database and then my program close. I'd like a msgbox to instruct the user in opening a proper database.

 If Not FilePathStr = "" And Not FileNameStr = "" Then
   On Error GoTo NotValidBase
   Set dbData = OpenDatabase(FilePathStr & "\" & FileNameStr)
   '
   dbData.Close
 End If
 Exit Sub
 '
NotValidBase:
 MsgBox "That's not a valid database !", vbExclamation
Avatar of MikeP090797
MikeP090797

You can use the common dialog control. Use .ShowOpen to select the files
Avatar of jfl

ASKER

???

I need to open a (*.mdb) Access database, but if that file is corrupted, my app will go down. How do I prevent that?
From where are you getting the values for FilePathStr and FileNamestr variables? does your form has drive listbox and file listbox? if it has those two, you can always present the user with valid .mdb extension files.

vmano
Avatar of jfl

ASKER

Yes I know, but what if the *.mdb file is corrupt ?
I don't see any problem with your code. If the OpenDatabase fails (for any reason), the error will be trapped. You should add a better error message

If Not FilePathStr = "" And Not FileNameStr = "" Then
   On Error GoTo NotValidBase
   Set dbData = OpenDatabase(FilePathStr & "\" & FileNameStr)
   '
   dbData.Close
   Set dbData = Nothing
 End If
 Exit Sub
 '
NotValidBase:

  MsgBox "Error : " & Err & " opening database." & chr$(13) & ERror, vbExclamation

Avatar of jfl

ASKER

I tried your code, but I still get an error message (Runtime error 5)
OK. you can trap the following three error  codes/messages and present the user with the same message box.

Error Number: 3197
Error Message: The Microsoft Jet database engine stopped the process because you and another user are attempting to change the same data at the same time.

Error Number:3343
Error Message: Unrecognized database format 'databasename.mdb'.
 
Error Number:3015
Error Message:'databasename.mdb' isn't an index in this table. Look in the Indexes collection of the TableDef object to determine the valid index names.

let me know if this helps,
vmano
Did you add the reference to the DAO?
Avatar of jfl

ASKER

The problem is not the error number but that I get a runtime error 5
ASKER CERTIFIED SOLUTION
Avatar of fulscher
fulscher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After MsgBox command simpl add
Resume next