Stop running macro if

maw230

somewhat competent
Local time
Today, 00:06
Joined
Dec 9, 2009
Messages
522
I need a VBA command to stop the current macro (created in Access, not VBA) if a condition is met.
All I have been able to find so far is to use the SendKeys command to send CTRL + BREAK, but I can't get that to work and it's certainly not the most ideal method.
 
Send keys is generally not recommended. It sounds like you are approaching your problem from the wrong angle. What are you trying to do? What does the macro do? When does it fire? When do you need it to stop?
 
Actually if I could just get the correct command to Stop or Exit the macro I would be fine. Here is my admittedly ghetto VBA:

Code:
Public Function TEST12()

Dim Check As Long

If Nz(DCount("Line", "qry_Product_Manager"), 0) > 0 Then
Check = 1
Else
Check = 2
End If

If Check = 1 Then
DoCmd.OpenQuery "qry_Product_Manager"
Else
[B]DoCmd.Close[/B] - Need command for STOP or EXIT here instead
End If

End Function
 
Did you try Exit Function? Is there more in this function you are not showing? Why use the variable check? Why not just:
Code:
if dcount...then
     docmd.openquery
else
     exit function
end if
 
Did you try Exit Function? Is there more in this function you are not showing? Why use the variable check? Why not just:

I use the variable check, because I'm going to need to check the same query again for records later on in the function/macro.

I don't seem to have an Exit Function that I can find. At least when I use "DoCmd."

Code:
DoCmd.Close
closes the macro, but for whatever reason it continued to run the next step even after the macro was "closed".
 
maw230, follow the syntax speakers_86 gave you in post #4.
 
Well, I need to exit the macro created in Access not the actual VBA function. Does that make sense? The macro was created in design view in Access and not in VBA. I'm going to convert it to VBA though which should make it much easier.
 
Actually if I could just get the correct command to Stop or Exit the macro I would be fine. Here is my admittedly ghetto VBA:

Code:
Public Function TEST12()
 
Dim Check As Long
 
If Nz(DCount("Line", "qry_Product_Manager"), 0) > 0 Then
Check = 1
Else
Check = 2
End If
 
If Check = 1 Then
DoCmd.OpenQuery "qry_Product_Manager"
Else
[B]DoCmd.Close[/B] - Need command for STOP or EXIT here instead
End If
 
End Function

Is the call to this Function only a part of a greater MS Access Macro, and therefore when it exits, the Macro should also exit? In that case, I believe that you can define a Global Return Status Variable that could be tested by the calling Macro, and the Macro could make the Decision based on the value of the Returned Status.
 
That is a good idea, though I can't help but wonder why the function and the macro are different objects.
 
Converting it to VBA is a more sensible option.

If you want to stop a macro you can use the StopMacro action.
 

Users who are viewing this thread

Back
Top Bottom