Solved

Open file -> Error

Posted on 1998-09-25
11
189 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

929 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

18 Experts available now in Live!

Get 1:1 Help Now