Handling Errors in VB/VBA/VBS/ASP

Real-life Samples (2)

A simple model to guarantee that a required file exists before opening the application.

In many events you may find a need to store user event driven data and option settings to an ASCII file. This guarantees a simple enough way to write log files, generate start up templates and other events which should be loaded at the time you run your Sub Main() or the start up form's Form1_Load event.

What you will soon notice is that if the required file doesn’t exists, your application will return an error 53 File does not exist. In order to solve this, you can use this model to design your proper start up sequence, thereby producing the required file on the first time your user starts the program.

In the use of and creation of source code like this you may find it useful to know a bit about how to open and load data from ASCII files, into your application, as well as how to save and output data from the same. I have previously posted a snippet about this on both my own web site and under this link, which may provide you further details in the matter.

First of all, locate the first starting form or function of your application. This would normally be Form1_Load. You can create a sub for this event by simply double clicking on a free area of your form design. In the event you have elected to use a Sub Main() to start your application, this code must be correctly amended thereto. In this sample I assume you are using Form1.

In the source code for Form1 include this code.

Private Sub Form1_Load()
Call LoadOptions()
End Sub


In a module, preferably the Module1 since this is in the beginning of the program, include the following code template.

Public Function LoadOptions()
On Error GoTo ErrHandler
Dim F As Integer, OptFile As String

OptFile = "X:\myfilepath\to the\optionfile.txt"
F = FreeFile


Open OptFile For Input As #F
'
' Read more about opening files and reading byte, line or specific length
' strings into a variable here. The examples given in the VB help files are excellent.
' Read the items to variables or set the options as they appear in your file. '

Close #F

Exit Function ' break out here if no error occurred
ErrHandler:
Select Case Err.Number
Case 53
    Err.Clear
        Call FirstStart ' the file didn’t exist so create it for the user
    Resume ' resume at the line where the error occurred
Case Else
    ' For simplicity here, let windows handle errors other than 53
End Select
Err.Clear
Resume Next
End Function


This function is a part containing base data, which are required options in your program. You could probably achieve this by setting a counter somewhere in the registry but consider the risk that a user by accident deleted the full options file.

If you then wish to have some control over how the initial start up is displayed, it may cause a conflict of the two interests. I will leave this detail and many more in this base model for your imagination and creativity to solve.

Public Function FirstStart()
'
' In this function generate the first base template of a saved options file
' you can also include code such as opening a help page for beginner's info
' or simply open notepad with a disclaimer and ReadMe file
'
End Function



A simple way to do DoesFileExist

You can call this function form a form or your code by passing it a fully qualified file path and file name. The return value will be True if the file is found and False if the file is not found.

Public Function DoesFileExist(ByRef FindFileName As String) As Boolean
On Error GoTo ErrHandler
Dim N As Long ' define a variable to use for a call

DoesFileExist = True ' initiate by assuming the file does exist

N = FileLen(FindFileName) ' call to retrieve the Len of the file

' if the previous statement throws an error 53 = file does not exist then ' call the error handler

Exit Function ' break out here if no error occurred
ErrHandler:
Select Case Err.Number
Case 53
    Err.Clear
    DoesFileExist = False
    Resume Next
Case Else
    ' For simplicity here, let windows handle errors other than 53
End Select
Err.Clear
Resume Next
End Function


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, without prior written statement form the author hereof.

The names referred to herein such as Visual Basic, MS, Microsoft, MS Office, VB and/or VBScript as well as other company names, copyrighted names, trademarks and so on are the property of their respective owners and are only included for the purpose of clarity in the contents description of this document.

Happy Coding and have fun - I sure did while writing this, but then again I am a netgeek and not a notgeek!

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates