How to Write an If Statement in Access

Save

"If" statements can evaluate "true" or "false" conditions and perform actions based on those conditions. These statements can be broken down into two types: "If-Then" statements and "If-Then-Else" statements. "If-Then" statements perform a set of actions only when the statement's conditions are met. "If-Then-Else" statements perform one set of actions if the statement's conditions are met and a different set of actions is the conditions are not met. "If" statements can be entered using the Visual Basic Editor or the Expression Builder.

Visual Basic If-Then Statements

  • Click the "Database Tools" Tab and select "Visual Basic." This will open the Visual Basic Editor.

  • Open the module to which you will be adding the "If" statement.

  • Click "Insert" on the menu bar, and then select "Procedure."

  • Type the name of the procedure in the name box.

  • Select "Sub" followed by either "Public" or "Private" for the scope.

  • Click "OK." This will open the editor box with "Sub" and the "Name" on the first line. Press Enter.

  • Type "If [Your condition here] Then" on the second line. Press Enter.

  • Enter the actions you would like function to perform if the result is evaluated as "true". Repeat this step as many times as needed, with each action on a separate line. Press Enter.

  • Type "End If." Press Enter.

  • Type "End Sub."

Visual Basic "If-Then-Else" Statements

  • Click the "Database Tools" tab, and then select "Visual Basic." This will open the Visual Basic Editor.

  • Open the module to which you will be adding the "If" statement.

  • Click "Insert" on the menu bar, and then select "Procedure."

  • Type the name of the procedure in the name box.

  • Select "Sub," and select either "Public" or "Private" for the scope.

  • Click "OK" to open the editor box with "Sub" and the "Name" on the first line. Press Enter.

  • Type "If [Your condition here] Then". Press Enter.

  • Enter the actions you want function to perform if the result is evaluated as "true". Repeat this step as many times as needed, with each action on a separate line. Press Enter.

  • Type "Else." Press Enter.

  • Enter the actions you want the function to perform if the result is evaluated as "False". Repeat this step as many times as needed, with each action on a separate line. Press Enter.

  • Type "End If". Press Enter.

  • Type "End Sub". Press Enter.

Expression Builder "If" Statement

  • Select the cell you are going to add the "If" statement, click the "Build" button. This will open the "Expression Builder."

  • Type "=IIF(the expression, TRUE condition, FALSE condition)".

  • Click "OK."

Tips & Warnings

  • Using the Expression Builder is good for simple "If" statements, but the Visual Basic Editor is better for the more complex "If" Statements.
  • IF statements can be nested to achieve multiple calculations or evaluations of data. Web databases limit nesting to 65 statements, but there are no nesting limits for client databases.
  • If using the Expression Builder, make sure to use the command "IIF" instead of "IF." Microsoft Access has used "IIF" as the function name for "If" statements.

Related Searches

References

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!