Recommendations For Writing VisualBasic For Applications (VBA) Code in RSViewSE
Recommendations For Writing VisualBasic For Applications (VBA) Code in RSViewSE
RSViewSE
This document is to highlight common errors, mistakes and general advice when writing VBA code
for RSViewSE. In the document below, when the acronym VBA is referred to, it means explicitly
VBA code for RSViewSE. Also refer to the extensive Microsoft help on the web for VBA coding
help and guidance.
1. Error Checking
Error checking must be enabled for all VBA code routines. The type of error checking that should
be used is based on the use of the VBA code in the routine. Having VBA code with no error
checking is at some point in time going to cause a problem, (even in the simplest routines), having
a problem in the VBA code is going to cause problems with the RSViewSE Client or even the
overall application.
When designing the error trapping try to think about the following:
What errors can I test for before an error occurs, e.g. when doing arithmetic could you get a
division by zero, so test for zero first.
What errors may occur but I should expect them? E.g. writing to a drive that maybe is a
floppy drive and is full.
What to do if an error occurs that I did not expect?
If an error does occur can operator intervention fix it?
How to prevent fatal errors that will crash the VBA code.
------------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.4
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 1 Of 7
11/12/2007
The On Error Resume Next should only be used in limited circumstances, the reason for this is
that if an error has occurred on a line of VBA code, you are continuing in an unknown state.
However in limited cases where you do not care if an error occurred or not then this can be used,
usually only for very short VBA code routines.
The recommended way is to use the On Error Goto Label. It is then possible to control either at a
routine level or higher level what the errors is, if you expected it and what to do about it.
As an example: The idea of the ErrorPosition variable is that you can make a reference as to what
VBA code is about to be executed, so that errors can be categorized and the handling of them
changed accordingly.
On Error GoTo MyErrorTrap
Dim ErrorPosition as Long Use this variable as an index to the position in VBA code.
ErrorPosition = 1
Some VBA code Lines
ErrorPosition = 2
Some More VBA code Lines
ErrorPosition = 3
Etc.
Exit Function or Sub
MyErrorTrap:
Log error to FT Event File for later analysis. To aid fault finding include the DisplayName.GFX, the
Function/Sub routine name and the ErrorPossition code to isolate where the error occurred.
LogDiagnosticsMessage "Graphic: <filename>, Function: <function name>, Code Position: & _
ErrorPosition & Error Code : [ " & Hex(Err.Number) & "], Description: " & Err.Description
Select Case ErrorPosition
Case 1
Decide What To Do, Were to return in VBA code.
Case 2
--------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.3
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 2 Of 7
17/12/2004
17/12/2004
If your objects are required for the duration of the display being open, then declare them
globally and de-reference when the display is closed.
If your objects are required for the duration of the SE Client life, then declare publicly on a
cached hidden display (so the display is not closed until the SE Client is stopped) and dereference only when the display closes as the SE Client is requested to stop.
--------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.3
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 4 Of 7
17/12/2004
Frequently the database is located on a separate PC to the one where your VBA code is
running, this means a network is involved and hence potential disconnections and other
network related issues, these can cause hang-ups and delays in the VBA code when
accessing database objects.
Whilst developing SE Client VBA code, the access to the database objects may be very fast
and not cause you any concern; however after the SE Client has been running for a while;
databases if not managed or index correctly can grow to a size where significant delays are
experienced, and hence response and hang-up problems can occur.
Be ready to handle database table or record locks. These can occur in several situations,
particularly multi-user environments. It is necessary to decide on the best action; retry, error
etc.
--------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.3
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 5 Of 7
17/12/2004
--------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.3
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 6 Of 7
17/12/2004
--------------------------------------------------------------------------------------------------------------------------------------------Recommendations For Writing VisualBasic for Applications (VBA) Code In RSViewSE V1.3
Andrew Crossland, Rockwell Automation, ACIG: European Center of Excellence
Page 7 Of 7
17/12/2004
ERROR: syntaxerror
OFFENDING COMMAND: --nostringval-STACK:
/Title
()
/Subject
(D:20080505174115)
/ModDate
()
/Keywords
(PDFCreator Version 0.8.0)
/Creator
(D:20080505174115)
/CreationDate
(acrossla)
/Author
-mark-