Help with Error Handling

G

Greg Maxey

Despite reading the help file until my eyes are blurry, I still can't get my
head around Error Handling.

I have some code provided below that can cause an error if the user inputs
and incorrect format. I would appreciate any feedback regarding the Error
Handling that I have used. While it appears to work, I am not sure if I
have left a door open somewhere that would incude other errors for fail to
detect other errors.

Thanks.


Dim myDate As Date
Dim seqNum As Long
Dim seqLength As Long
Dim seqInt As Long
Dim upSeq As Long
Dim i As Long
Dim dateFormat As String

Retry:
On Error GoTo Oops
myDate = InputBox("Enter the start date in 1/31/05 format.", _
"Start On", Date)
seqLength = InputBox("Enter the sequence length e.g., 14.", _
"Sequence Length", "14")
dateFormat = InputBox("Enter the format for the date display e.g." _
& "dddd, mmm dd yyyy.", "Date Format", "mmmm dd, yyyy")
seqInt = InputBox("Enter the sequence interval.", "Interval", "1")

For seqNum = 1 To seqLength
upSeq = seqNum * seqInt
ActiveDocument.Variables("Var" & seqNum).Value = _
Format(myDate + upSeq - seqInt, dateFormat)
Next
ActiveDocument.Fields.Update
Exit Sub
Oops:
If Err.Number = 13 Then
If MsgBox("Invalid format! Start date must be in 1/31/2005 format", _
vbRetryCancel, "Alert") = vbRetry Then
Resume Retry
End If
Resume Next
End If
End Sub
 
H

Helmut Weber

Hi Greg,

you don't need help, you need someone to discuss things with.

Welcome anyway.

1st, it is alomst impossible to input incorrect date format.
You macro processes, it seems, any integer,
here and now with my German version.

2nd,
"Enter the start date in 1/31/05 format."
and
"...Start date must be in 1/31/2005 format"
seem to be inconsistent in a way, if I may say so.

3rd, Oops handles error 13 only. How about other errors?
But as I wasn't able to provoke any other error
from the user interface,
error 13 may be the one and only error that ever could occur.
Can you prove that?
If so, to get the error number would be a waste of time,
of not much time, however.

Greetings from Bavaria,
a region in Europe,
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg Maxey

Helmut,

Someone to discuss things with is nice, but I need help ;-)

1. I find it easy to generate an error with the date input. With my clunky
fingers
1/312/05 is not a challenge. Still a more appropriate prompt might be:

myDate = InputBox("Enter the start date in any standard" _
& " format e.g. " & Date & ".", "Start On", Date)

With the defualt being today's date in 01/08/31 format

2. Thanks for pointing out the disparity in input and error message box
formats. Changed to:

If MsgBox("Invalid format! Try something like " & Date & ".", _
vbRetryCancel, "Alert") = vbRetry Then
Resume Retry

3. No I can't prove the code is limited to error 13. That's one of the
parts about error handling that I don't understand. I see how my original
code was limited to error 13, but I don't know how to deal with other errors
that occur. I changed the handler like so:

Oops:
If Err.Number = 13 Then
If MsgBox("Invalid format! Try something like " & Date & ".", _
vbRetryCancel, "Alert") = vbRetry Then
Resume Retry
End If
Else
MsgBox "Unantticipated error " & Err.Number & ": " & _
"Err.Description, vbExclamation"
Resume Next
End If

Thanks for the help.

Whole code follows:

Sub DateSeq()
Dim myDate As Date
Dim seqNum As Long
Dim seqLength As Long
Dim seqInt As Long
Dim upSeq As Long
Dim i As Long
Dim dateFormat As String

Retry:
On Error GoTo Oops
myDate = InputBox("Enter the start date in any standard" _
& " format e.g. " & Date & ".", "Start On", Date)
seqLength = InputBox("Enter the sequence length e.g., 14.", _
"Sequence Length", "14")
dateFormat = InputBox("Enter the format for the date display e.g." _
& "dddd, mmm dd yyyy.", "Date Format", "mmmm dd, yyyy")
seqInt = InputBox("Enter the sequence interval.", "Interval", "1")

For seqNum = 1 To seqLength
upSeq = seqNum * seqInt
ActiveDocument.Variables("Var" & seqNum).Value = _
Format(myDate + upSeq - seqInt, dateFormat)
Next
ActiveDocument.Fields.Update
Exit Sub
Oops:
If Err.Number = 13 Then
If MsgBox("Invalid format! Try something like " & Date & ".", _
vbRetryCancel, "Alert") = vbRetry Then
Resume Retry
End If
Else
MsgBox "Unantticipated error " & Err.Number & ": " & _
"Err.Description, vbExclamation"
Resume Next
End If
End Sub
 
H

Helmut Weber

Hi submariner,

that is about the best you can do, I'd say,
not to speak of calling it "unkown error" or
"error number 1" on the macintosh, which I abhor anyway.

Have a nice day.

Greetings from Bavaria,
a region in Europe,
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top