Solved

VB/VBA Puzzle

Posted on 2011-02-11
4
398 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

777 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