?
Solved

Open file -> Error

Posted on 1998-09-25
11
Medium Priority
?
194 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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