From the course: Access: VBA

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Perform a task when conditions are met using an If…Then statement

Perform a task when conditions are met using an If…Then statement

From the course: Access: VBA

Start my 1-month free trial

Perform a task when conditions are met using an If…Then statement

- [Teacher] So far in this course, most of the example code has contained subroutines or functions that run every time you call them. There will be times though, when you want part of your code to run. Only when a particular condition is true. You can use if, then statements, to execute code conditionally. I'll demonstrate how to do that in this movie. I have opened the database sample file, and I'll press alt f11 to move to the visual basic editor. I want to work within a code module, so I will click insert module, and there we go. I'll define this subroutines, I'll type sub and I'll just call it if, test, followed by open and close parenthesis with nothing in the middle, 'cause I'm not passing any data into the sub routine. Now I can declare my variables so I'll do Dim-c-u-r-Total, which will represent the total of a sale, as currency. And a comma and the commission will also be a currency. So c-u-r-C-o-m-m as currency. Then, enter. We'll start by getting the total of the sale…

Contents