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
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
You can use the common dialog control. Use .ShowOpen to select the files
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?
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
vmano
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
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
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
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'
let me know if this helps,
vmano
Did you add the reference to the DAO?
ASKER
The problem is not the error number but that I get a runtime error 5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After MsgBox command simpl add
Resume next
Resume next