Cancel a function

B

BruceM

I'm not quite sure what to ask. I have a public function that, depending on
values such as expiration date, generates one of several text strings that
are used on faxes. Here is a shortened version:

Public Function FaxText(NoExp As Boolean, CertType As String) As String

' Function is used as the control source for a text box on the fax
(report)
' that reminds vendors a cert has expired

Dim intStatus As Integer

' CertReturned = True until a cert has been requested. CertReturned =
True _
' for certs that are in good order (i.e. still current or pending)
If NoExp = False Then
If CertType <> "Survey" Then
intStatus = 1
Else
intStatus = 2
End If
Else
intStatus = 3
End If

Select Case intStatus
Case 1
FaxText = "Option 1"
Case 2
FaxText = Option 2"
Case 3
FaxText = "Option 3"
End Select

The Control Source of a text box on a report is:
=FaxText([NoExp],[CertType])

The thing is that I would like to add in some places the equivalent of
Cancel = True. The actual function has several more condtions than does
this example, so maybe the need for Cancel does not arise here, but it does
in the real function. The trouble is that I get a Variable not Defined
error when I compile if I try to use Cancel = True. I suppose I need to add
Cancel as Integer at the beginning of the function, but I can't figure out
where to put it. Or maybe I'm not even close.
 
B

Barry Gilbert

The Cancel parameters is only used in Event handlers. Have you tried "Exit
Function"?

Barry
 
B

BruceM

Thank you for taking the time to respond. The trouble I was having was that
some of the conditions that should have produced intStatus = 3 and 4 were
also part of the conditions for intStatus = 7 and 8. I tested with a
message box and found that the intStatus = 3 conditions were being met as
expected, but they were also met by intStatus = 7 and 8. Since intStatus =
8 was later in the code, the corresponding text string was being produced by
the function. My idea was that if I could stop the code if the intStatus =
3 conditions are met it would never run through to the intStatus = 8
condition. However, Cancel = True (or Exit Function) is, I see now, the
wrong approach, since the function never runs to the Select Case section.
I ended up making the intStatus = 7 and 8 conditions more restrictive, and
it works as it should now, but I wonder if I could have stopped the code as
soon as a condition is met. Once the intStatus = 3 conditions are met, skip
the rest of the tests and go directly to the Select Case section. I have
heard that GoTo should be avoided except for error handling, so I'm not sure
what else I can do.
I can post the full code if it would help.

Barry Gilbert said:
The Cancel parameters is only used in Event handlers. Have you tried "Exit
Function"?

Barry

BruceM said:
I'm not quite sure what to ask. I have a public function that, depending
on
values such as expiration date, generates one of several text strings
that
are used on faxes. Here is a shortened version:

Public Function FaxText(NoExp As Boolean, CertType As String) As String

' Function is used as the control source for a text box on the fax
(report)
' that reminds vendors a cert has expired

Dim intStatus As Integer

' CertReturned = True until a cert has been requested. CertReturned
=
True _
' for certs that are in good order (i.e. still current or pending)
If NoExp = False Then
If CertType <> "Survey" Then
intStatus = 1
Else
intStatus = 2
End If
Else
intStatus = 3
End If

Select Case intStatus
Case 1
FaxText = "Option 1"
Case 2
FaxText = Option 2"
Case 3
FaxText = "Option 3"
End Select

The Control Source of a text box on a report is:
=FaxText([NoExp],[CertType])

The thing is that I would like to add in some places the equivalent of
Cancel = True. The actual function has several more condtions than does
this example, so maybe the need for Cancel does not arise here, but it
does
in the real function. The trouble is that I get a Variable not Defined
error when I compile if I try to use Cancel = True. I suppose I need to
add
Cancel as Integer at the beginning of the function, but I can't figure
out
where to put it. Or maybe I'm not even close.
 

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