Conditional Statements

One of the goals of computer programming is telling the computer what to do when something occurs, and how to do it. This is performed by setting conditions, examining them and stating what decisions the computer should make. To perform the necessary conditions, you have two main options: Microsoft Access or Microsoft Visual Basic. Microsoft Access is equipped with a series of operators and functions destined to perform various operations. To use a condition in Microsoft Access, if you know the structure of the conditional statement, you can write it after typing an assignment operator. Because most conditions in Microsoft Access are in the form of functions, we will study them later on. For now, we will learn how to write conditions in the Microsoft Visual Basic language.
Topics: Topics: Logical Operators, The If...Then Statement, The If...Then...Else Statement, The If...Then...ElseIf Statement, The Select Case Statement, Looping and Counting.

Sponsored Links:

Related Topics

Programming Tools
I am looking for a capability that I used some time ago. It was a program documentation tool that blocked out your code so that it became easier to see where loops, conditional statements etc. begin and end.

An associate has requested help with existing code that needs to be maintained and which is long and where conditional statements exceed 1 or 2 screen pages, are not well blocked or documented as a starting point to begin to understand how the code works


Conditional Formatting Access 2007 Form
When attempting to use the conditional formatting button; I am limited to 3 different equal to options. I have several list items in the field and would like to group two or three items in the same conditional formatting. (I.e. Expired, Removed, Deleted all have Gray field with black text.)

I have attempted to use “Expired” Or “Removed and “Expired” And “Removed” but neither option works. Is there a way to use conditional formatting with And\Or statements?


Create conditional (Boolean) expressions
This article explains how to create conditional (also known as Boolean) expressions. A conditional expression evaluates to either true or false, and it then returns a result that meets the condition that you specify. If you use functions in your conditional expressions, you can also specify an action for values that do and do not meet the condition in the expression. For example, you can create an expression that finds all sales with a profit margin of 10 percent or less, and then specify that those numbers appear in red type, or replace the values with a message.
In this article
* Understand conditional expressions
* Create a conditional expression
* Examples of conditional expressions


Conditional formatting inside a form
I am trying to set a conditional format for a required field based on the value of another field from the same table. I am trying to do this from the form that inputs the data in to the table.

In other words, if the value of field A = 2, I want the value of the required field to be "yes" or "no" (this is a user input). If the value of field A is not 2, then the value of the required field is null.

I've been experimenting with Iif statements for several hours and I'm at a loss. I am familiar with the basics of SQL but this seems to be beyond my expertise.


Conditional formatting inside a form
I am trying to set a conditional format for a required field based on the value of another field from the same table. I am trying to do this from the form that inputs the data in to the table.

In other words, if the value of field A = 2, I want the value of the required field to be "yes" or "no" (this is a user input). If the value of field A is not 2, then the value of the required field is null.

I've been experimenting with Iif statements for several hours and I'm at a loss. I am familiar with the basics of SQL but this seems to be beyond my expertise.


VBA conditional formatting
I have read up about conditional formatting and understand the *concept* clearly (I think). However, is it possible to display conditional formatting of a subform in datasheet view on a form? If so, as I need to have more than 3 (or 4 if default is used) colours?


Conditional Formatting vs. VBA Formatting
I have been researching this for some time now but haven't received any sound advice on whether it is faster to perform formatting on text boxes in a report via VBA or the built in Conditional Formatting.

The reason I ask this is that I have a report with quite a few text boxes that currently all have some sort of Conditional Formatting on them (Not all have the same Conditional Formatting). I know that when you only have a few controls that require Conditional Formatting, the built in solution in Access 2007 should be sufficient, however when the number of controls grows and when the report has multiple records that require each control to be handled on each record, the Conditional Formatting becomes very slow (much like receiving 1000 paper cuts) and renders the report as pointless considering a user must run it for 10's of minutes to an hour to complete.

So, with that said, I am really just looking for anyone's suggestion as to whether I should invest some time in the VBA coding to do what the Conditional Formatting does, or do I just go back to the requestor and let them know that formatting all these fields is too much for the system to handle.


Change the appearance of a control by using conditional formatting
Conditional formatting allows you to selectively highlight certain data on your form or report so that it is easier to understand. For example, you might want to format negative numbers in a red font to make it clear that those records need further attention.
You can set conditional formatting for a text box or a combo box control. You can change the formatting based on the control's own value, or you can use an expression to change the formatting based on the values contained in other fields or controls. On a form, you can use conditional formatting to disable a control, and you can cause the formatting of a control to change when the cursor is positioned in that control.
What do you want to do?:
*Apply conditional formatting to a control based on its own value
*Use an expression to apply conditional formatting to one or more controls
*Change the formatting of a control on a form that has the focus
*Remove conditional formatting from one or more controls
*Create alternating row colors on a report


Conditional Formatting a date
I am stuck on conditional formatting of a date. A field on a subform is auto populated with the now() command, and gives the following output: 23/01/2012 16:16:17

I am trying to apply conditional formatting to that field so that it turns a different colour if the date is the same as todays date. I have tried using date() in both the expression, and 'is like' section of conditional formatting, but it is notpicking it up becasue of the time value.

I think I need some sort of contains, or find command, but not sure of how to do this


Conditional Formatting on client
I have a report that uses conditional formatting to shade controls It runs without problem on access 2000, 2002, 2003 and 2007

Two 2003 clients have problems with the conditional formatting. No matter what I put in for an expression it formats as if the criteria is not met even Expression TRUE | or expression 1=1 won't format the controls

The expressions I have work fine for all the other clients (about 20 of them). What settings on that client could cause it to ignore conditional


Conditional Formatting in a Sub Report
I'm struggling with a problem I'm having with conditional formatting in an access sub report. I have a main report and 9 sub reports (Monday-Sunday + Total + Targets) each linked to a date on the main form. Each value in the sub reports are compared with the values in the targets sub report and conditional formatting applied.

Basically everything works fine in Report View but conditional formatting disappears in print preview. Any ideas? (Back style is set to Normal


Conditional Formating (Report Field)
Access allows you to conduct conditional formatting on a report, or form field. The unfortunate part is that they only allow 3 conditional formats. I am unfortunatly not a programmer, and wondered if anyone could provide me with a simple code to add as many conditional formats. I am only looking to change the "Back Color". For instantce; If the filed says "RED" I would like the back color to be RED, and so on. I need to be able to add as many colors as I want


Conditional Formatting through VBA: On Undo
I've moved all my conditional formatting to VBA because of performance issues with Access's built-in conditional formatting facility, but there's one feature I can't replicate. Some of my formatting involves highlighting fields if they're blank.

This works fine for On Current and After Update, but I can't get it to work for On Undo: if I undo changes and thereby leave a field blank, I can't get the conditional formatting to kick in again.

Am I missing something about On Undo, or is there another way of doing this?


Conditional Formatting through VBA: On Undo
I've moved all my conditional formatting to VBA because of performance issues with Access's built-in conditional formatting facility, but there's one feature I can't replicate. Some of my formatting involves highlighting fields if they're blank. This works fine for On Current and After Update, but I can't get it to work for On Undo: if I undo changes and thereby leave a field blank, I can't get the conditional formatting to kick in again. Am I missing something about On Undo, or is there another way of doing this?


Multiple IIF statements
I'm trying to compile a number of IIF statements. I have 6 IIF statements, but would like to put them all in to one IIF statement.


conditional numeric formatting of field output on report
I have a report that uses one area to display either a percentage or a count.

I'd like to format the report field output so that if the value is less than 1 to format the number as a 2 decimal point percent and if it is 1 or greater format it as an integer

the conditional formatting features I see deal with the conditional display format and not the conditional numeric formatting.


conditional formatting
boys, I need help with conditional formatting.
I have two date fields. scheduled date and place.
when the scheduled date is less than today, I put a conditional formatting.
how do I date field when the other place is filled, remove the conditional formatting of the other field.


Highlight values on a report by using conditional formatting
This video walks you through the process of applying conditional formatting to a column of numbers on a report. By using conditional formatting, you can highlight specific values automatically, making your report easier to understand.


#Name? Conditional Formatting Report
Full disclosure: I am an Access wannabe. I also know this should be easy, but I'll be danged if I can figure it out.

I have a report that uses a query, which combines several tables. Within that report I am trying to apply conditional formatting to a field called StatNam. Without the conditionalformatting, the results display just fine.

When I apply the conditional format 'Field Value is equal to'.then enter 'Green' (which is a valid value) the report displays "#Name?".

I changed the Field Name text box to make sure there was no conflict there, but I get the same result. If I remove the conditional formatting, the report will display the correct value (e.g., 'Green').

For grins I created a report directly from a table and the conditional formatting for StatNam worked just fine.


Using If Statements
Read this article and learn how to Use If Statements. (video tutorial)