If Statement and macro

L

Lolli

Hi, does anyone know if it is possible to put a ctrl function into an if
statement so that if the statemnt is true the ctrl function will run a macro,
and if not true, it does nothing?
 
J

John B. Smotherman

Are you asking if you can assign a condition to a control on a form? If so,
the answer is "yes". Here are the steps I took to do this.

Open one of your modules in the Visual Basic window and enter a new function
for calling the macro. I named mine CallMacro. Here's the code I used:

Function CallMacro ( strMacro as String, bFlag as Boolean)
If bFlag Then
DoCmd.RunMacro strMacro
End If
End Function

Now, open the form in design view, make sure the properties window is
displayed, and click on the control. In the OnClick event, click the event
builder button (the one with the three dots) and choose Expression Builder.
Enter the following:

=IIf(<CONDITION>,CallMacro(<MACRO_NAME>, True),CallMacro("", False))

Simply substiture your condition and your macro name for the <CONDITION> and
<MACRO_NAME> above, and give it a try.

HTH
 
C

Clifford Bass

Hi John,

Nice. How about a slight simplification?:

=CallMacro(<MACRO_NAME>, <CONDITION>)

Clifford Bass
 
J

John B. Smotherman

Of course! Isn't it interesting how your mind locks into a solution based on
the question. "Can you put a control into an if statement?"

Thanks!
 
Top