Solved

VB/VBA Puzzle

Posted on 2011-02-11
4
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 85
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

724 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