Solved

VB/VBA Puzzle

Posted on 2011-02-11
4
407 Views
Last Modified: 2012-06-21
Hi guys.

I have one vba routine that calls another.  When the other routine is called a check is run and, if this check fails (ie is FALSE) I want it to halt running code altogether.  At the moment all I can get it to do is to stop the other routine with 'Exit Sub', but the code then returns to the original calling procedure and continues to run.

How would I go about doing this properly?

Thanks in advance.
Matt
0
Comment
Question by:2toria
4 Comments
 
LVL 10

Accepted Solution

by:
VTKegan earned 500 total points
ID: 34871317
If the second routine is returning false, then in the first routine which calls the check you should use something like

If RunCheckRoutine <> False Then
  'Keep running your code

Else
  Exit Sub

End If
0
 
LVL 8

Author Closing Comment

by:2toria
ID: 34871397
Great idea - I managed to use this kind of thinking to work around the problem.  Cheers.
0
 
LVL 84
ID: 34871407
Note also that you must use Functions in order to "return" a value:

Function SomeFunction() As Boolean
  '/code here
  SomeFunction = False
End Function

Then call it like this:

If SomeFunction = True Then
  '/run your other code here
End If
0
 
LVL 28

Expert Comment

by:omgang
ID: 34871449
Expanding on what VTKegan said, you'll want to change the second routine from a Sub to a Function and set a Function return value of Boolean

So instead of

Private Sub SecondRoutine()

change it to

Private Function SecondRoutine() As Boolean


    'here's where the check is performed
    If Check = False Then
        SecondRoutine = False
        Exit Function
    Else
        SecondRouting = True
    End If

End Function


Then you call it from you first routine exactly like VTKegan suggests

If SecondRoutine <> False Then
    etc.


OM Gang
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question