If Statement and macro

  • Thread starter Thread starter Lolli
  • Start date Start date
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?
 
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
 
Hi John,

Nice. How about a slight simplification?:

=CallMacro(<MACRO_NAME>, <CONDITION>)

Clifford Bass
 
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!
 

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

Back
Top