Solved

Open file -> Error

Posted on 1998-09-25
11
188 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now