Solved

Open file -> Error

Posted on 1998-09-25
11
190 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:jfl
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1496400
You can use the common dialog control. Use .ShowOpen to select the files
0
 

Author Comment

by:jfl
ID: 1496401
???

I need to open a (*.mdb) Access database, but if that file is corrupted, my app will go down. How do I prevent that?
0
 
LVL 3

Expert Comment

by:vmano
ID: 1496402
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
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:jfl
ID: 1496403
Yes I know, but what if the *.mdb file is corrupt ?
0
 
LVL 14

Expert Comment

by:waty
ID: 1496404
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

0
 

Author Comment

by:jfl
ID: 1496405
I tried your code, but I still get an error message (Runtime error 5)
0
 
LVL 3

Expert Comment

by:vmano
ID: 1496406
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
0
 
LVL 14

Expert Comment

by:waty
ID: 1496407
Did you add the reference to the DAO?
0
 

Author Comment

by:jfl
ID: 1496408
The problem is not the error number but that I get a runtime error 5
0
 
LVL 12

Accepted Solution

by:
fulscher earned 10 total points
ID: 1496409
Use the RepairDatabase command (and also a CompactDatabase) before opening the DB. That's anyway a good idea to keep the DB size small. For 100 points, I can provide sample code.

From VB documentation:

RepairDatabase Method

DBEngine Object.

Attempts to repair a corrupted database that accesses the Microsoft Jet database engine.

DBEngine.RepairDatabase dbname

The dbname placeholder represents a string expression that is the path and filename for an existing Jet engine database file. If you omit the path, only the current directory is searched. If your system supports the uniform naming convention (UNC), you can also specify a network path, such as: "\\MYSERVER\MYSHARE\MYDIR\MYDB.mdb".

You must close the database specified by dbname before you repair it. In a multiuser environment, other users can’t have dbname open while you’re repairing it. If dbname isn’t closed or isn’t available for exclusive use, an error occurs.

This method attempts to repair a database that was marked as possibly corrupt by an incomplete write operation. This can occur if an application using the Jet database engine is closed unexpectedly because of a power outage or computer hardware problem. The database won’t be marked as possibly corrupt if you use the Close method or if you quit your application in a usual way.

The RepairDatabase method also attempts to validate all system tables and all indexes. Any data that can’t be repaired is discarded. If the database can’t be repaired, a trappable error occurs.


0
 

Expert Comment

by:PSV
ID: 1496410
After MsgBox command simpl add
Resume next


0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question