error control

I have a program that is using multiple forms. The main is actually just a menu to the others. I use on click frmXXXX.show to get to each different form. Now I'm trying to do some error control in case something is amiss and the text files that the forms feed from are missing. Don't want the program to freeze! Anyway, what I want is a msgbox to appear that says that file is missing and for the whole program to shut down. I've got the msgbox working, and have tried a few different ways but can't seem to get everything to shut down properly without errors. First one that got me was once the msgbox was displayed and you pressed ok then an error says something about the form already being loaded. Remember as I mentioned earlier, I used the frmXXX.show to open the frm which uses the text file. Included is the code I'm working on so maybe you can figure it out!!! TIA
On Error GoTo ICanHandleThisError
    Randomize
    Dim QuestionNumber As Integer
    QuestionNumber = 1

    Open App.Path & "\" & "BegTest.txt" For Input As #1
    Do While Not EOF(1)

        Input #1, m_strCorrectAn(QuestionNumber), _
                m_strQuestion(QuestionNumber), _
                m_strAnswer1(QuestionNumber), _
                m_strAnswer2(QuestionNumber), _
                m_strAnswer3(QuestionNumber)

        QuestionNumber = QuestionNumber + 1
    Loop

    Close #1
ICanHandleThisError:
            MsgBox "A file required by the " & _
                    "VB Skill Tester " & vbCrLf & _
                    "is missing. Please ensure that " & vbCrLf & _
                    "BegTest.txt is in " & vbCrLf & _
                    "the VB Tester directory "
           
            Exit Sub
seeshelleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

seeshelleAuthor Commented:
Oh yes, with the code as it is, the error msgbox will be displayed and then the test form will load without its contents! But it doesn't generate any errors this way.
0
KDivadCommented:
Instead of formXXXX.show, try using Load formXXXX and in the form's load event, if everything goes correctly, THEN use formXXXX.show or Me.Show, otherwise, display the msgbox and unload the form.
0
KDivadCommented:
The reason the msgbox is showing even without errors is:

    Close #1

>--Right Here--<

ICanHandleThisError:

Your code is dropping through to the errorhandler. You need to place an Exit Sub line where I have typed Right Here.

Here's your code modified a little:

Load FormXXXX

Sub FormXXXX_Load()

On Error GoTo ICanHandleThisError

    Randomize
    Dim QuestionNumber As Integer
    Open App.Path & "\" & "BegTest.txt" For Input As #1
    Do While Not EOF(1)

        QuestionNumber = QuestionNumber + 1
        Input #1, m_strCorrectAn(QuestionNumber), _
                m_strQuestion(QuestionNumber), _
                m_strAnswer1(QuestionNumber), _
                m_strAnswer2(QuestionNumber), _
                m_strAnswer3(QuestionNumber)

    Loop

    Close #1

    ME.SHOW
    EXIT SUB

ICanHandleThisError:

    MsgBox "A file required by the " & _
        "VB Skill Tester " & vbCrLf & _
        "is missing. Please ensure that " & vbCrLf & _
        "BegTest.txt is in " & vbCrLf & _
        "the VB Tester directory " 
    Set FormXXXX = Nothing

End Sub
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mark2150Commented:
You also want to take the error indicator off before doing much of anything else.

ICanHandleThisError:
  Resume ICanHandleThisError2
'
ICanHandleThisError2:
....


This will clear the "I'm in an error routine" flag so if your MsgBox and related code tosses an error you can recover more gracefully.

Actually if you can be killed by a missing data file your initialization code should look for the files and verify that they're there instead of letting a run-time error happen.

Try:

if len(dir( App.Path & "\BegTest.txt" )) < 1 then goto Oops_no_file

This is cleaner and more graceful.

M
0
cvidlerCommented:
mark2150 is right - don't wait for an error to happen, do the checking yourself in your main form this way the user in informed of the problem without having to wait through the load time of the main form and each other form in turn.
0
moterkCommented:
Do not create an error in the first place use:

TestFile$=dir(App.Path & "\" & "BegTest.txt")

then use:

if TestFile$="" then
    'no file code
else
    'open and process code here
end if

0
mark2150Commented:
moterk,

It's considered "bad form" to parrot an existing comment and post it as an answer.

Your answer is almost identical to my suggestion:

if len(dir( App.Path & "\BegTest.txt" )) < 1 then goto Oops_no_file

M
0
moterkCommented:
Sorry Mark2150

I just read up to

ICanHandleThisError:
  Resume ICanHandleThisError2
'
ICanHandleThisError2:
.....

and figured you were also discussing handling an error not preventing one.  I should have read your whole comment.

seeshelle should reject my answer and give you the points

0
seeshelleAuthor Commented:
Mark could you elaborate on this a little. I'm a newbie to programming and could use a little plain english.HAHA

if len(dir( App.Path & "\BegTest.txt" )) < 1 then goto Oops_no_file

basically I guess what this does is says if the length of the filename is less than 1 then go to OOps_no_file. right???
But then what???
Is there a way that I can just make an empty one, then let the user know that they'll have to enter some data into it??
0
mark2150Commented:
What you do in Oops_no_file is up to you. You get there when you're missing your data file. Typically there are two options: 1) Pitch a fit to the operator and shut down - "ERROR: DATA FILE MISSING - ABORTING" or 2) Politely inquire if the operator would like you to create a new file from scratch or perhaps search for the file somewhere else.

If the user picks "Create New" then flip 'em to a data entry screen to build the file from. No biggie.

M
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.