Solved

VB/VBA Puzzle

Posted on 2011-02-11
4
367 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
Comment Utility
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
Comment Utility
Great idea - I managed to use this kind of thinking to work around the problem.  Cheers.
0
 
LVL 84
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now