Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Open file -> Error

Posted on 1998-09-25
11
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 40 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

618 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