Stop Macro in VBA

G

Guest

I run this module from a macro. It checks an input form for blank records.
The code works exactly how I want it to work, but I need to stop the macro at
the end of this code. How can I do it?

Function CheckField()


If IsNull(Forms![frmProjectNew]![Project]) Then

Forms![frmProjectNew]![Project].BackColor = 3937500

End If

If IsNull(Forms![frmProjectNew]!
) Then

Forms![frmProjectNew]!
.BackColor = 3937500

End If

MsgBox "The fields in red are required fields", _
vbOKOnly, "Required Field"


End Function​
 
G

Guest

Not sure what you mean by stoping the macro at the end of this code?

If there is a loop and the macro keep running, then what is the code you are
using to run the Macro, mybe there is an endless loop there.
 
J

John Spencer

SInce you are running the function from a macro, I would suggest that you
change the function to return true or false

Function CheckField() as Boolean
Dim tfContinue as Boolean
tfContinue = True
If IsNull(Forms![frmProjectNew]![Project]) Then
Forms![frmProjectNew]![Project].BackColor = 3937500
tfContinue = False
End If

If IsNull(Forms![frmProjectNew]!
) Then
Forms![frmProjectNew]!
.BackColor = 3937500
tfContinue = False
End If

If tfContinue = false then
MsgBox "The fields in red are required fields", _
vbOKOnly, "Required Field"
End if

CheckField=tfContinue
End Function

Then add a condition to your macro
Condition: CheckField() = False
Action: Stop Macro

If Checkfield returns False then the action will run Stopping the macro if
Checkfield returns True then the Action (Stop Macro) will not run and the
next line in the macro will run.
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..​
 
G

Guest

Thanks for your input. I did something different that works but I think it is
the same concept as the one you described. Before running the macro, I check
for a condition, if it's true I run my script if not, I run a different macro.

John Spencer said:
SInce you are running the function from a macro, I would suggest that you
change the function to return true or false

Function CheckField() as Boolean
Dim tfContinue as Boolean
tfContinue = True
If IsNull(Forms![frmProjectNew]![Project]) Then
Forms![frmProjectNew]![Project].BackColor = 3937500
tfContinue = False
End If

If IsNull(Forms![frmProjectNew]!
) Then
Forms![frmProjectNew]!
.BackColor = 3937500
tfContinue = False
End If

If tfContinue = false then
MsgBox "The fields in red are required fields", _
vbOKOnly, "Required Field"
End if

CheckField=tfContinue
End Function

Then add a condition to your macro
Condition: CheckField() = False
Action: Stop Macro

If Checkfield returns False then the action will run Stopping the macro if
Checkfield returns True then the Action (Stop Macro) will not run and the
next line in the macro will run.
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

djf said:
I run this module from a macro. It checks an input form for blank records.
The code works exactly how I want it to work, but I need to stop the macro
at
the end of this code. How can I do it?

Function CheckField()


If IsNull(Forms![frmProjectNew]![Project]) Then

Forms![frmProjectNew]![Project].BackColor = 3937500

End If

If IsNull(Forms![frmProjectNew]!
) Then

Forms![frmProjectNew]!
.BackColor = 3937500

End If

MsgBox "The fields in red are required fields", _
vbOKOnly, "Required Field"


End Function

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top