473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Using OR in If statement, please help

Hi everyone,
I am trying to write a code so that when the user checks on one of those 3 fields, lblARF turn into red color, else stay black. I've placed the following code in the "On Current" event of the form, and also on the "On Click" event for all 3 fields. However, for some reasons, it didn't work quite well as I expected.
Can someone please help? Thanks!

Expand|Select|Wrap|Line Numbers
  1. Me.lblARF.ForeColor = IIf((Me.ARF_HYPO = Yes Or Me.ARF_NEW_OLI = Yes Or Me.ARF_AMINO_USE = Yes), vbRed, vbBlack)
  2.  
.
lblARF is the lable
ARF_HYPO, ARF_NEW_OLI, ARF_AMINO_USE are Yes/No field.

thanks!
Oct 8 '08 #1
2 1297
Stewart Ross
2,545 Expert Mod 2GB
Hi, and Welcome to Bytes!

In VBA boolean values are represented by the constants True and False, not Yes and No (which are treated as the names of variables - undeclared ones in this case). Rewriting your IIF using True to replace Yes and bracketing to make clear which part is which:
Expand|Select|Wrap|Line Numbers
  1. Me.lblARF.ForeColor = IIf(((Me.ARF_HYPO = True) Or (Me.ARF_NEW_OLI = True) Or (Me.ARF_AMINO_USE = True)), vbRed, vbBlack)
However, as the checkboxes return True or False we don't need to compare those to True at all:
Expand|Select|Wrap|Line Numbers
  1. Me.lblARF.ForeColor = IIf((Me.ARF_HYPO  Or  Me.ARF_NEW_OLI  Or Me.ARF_AMINO_USE), vbRed, vbBlack)
In VBA it is stylistically better not to use in-line IFs at all, so how's about using
Expand|Select|Wrap|Line Numbers
  1. IF (Me.ARF_HYPO  Or  Me.ARF_NEW_OLI  Or Me.ARF_AMINO_USE) THEN
  2.     Me.lblARF.ForeColor = vbRed
  3. ELSE 
  4.     Me.lblARF.ForeColor = vbBlack
  5. END IF
If you want to do away with repeatedly referring to the control for the forecolor property you could use a With statement as follows:
Expand|Select|Wrap|Line Numbers
  1. With Me.lblARF
  2.   IF (Me.ARF_HYPO  Or  Me.ARF_NEW_OLI  Or Me.ARF_AMINO_USE) THEN
  3.       .ForeColor = vbRed
  4.   ELSE 
  5.       .ForeColor = vbBlack
  6.   END IF
  7. END WITH
Finally, instead of placing the code in each event make it a private procedure and call that from each of the other event procedures - the After Update events for each of the controls (instead of On Click), and the Form Current event for the form itself.

-Stewart

ps should you find references to them at some stage the VB constants vbYes and vbNo are used for message boxes etc and are NOT synonyms for the boolean values True and False.
Oct 8 '08 #2
NeoPa
32,556 Expert Mod 16PB
It is always a good idea to ensure that variable name checking is enabled, AND your code compiles (at least compilation has been attempted), before submitting a question.

This avoids asking questions which are much more easily resolved on your own PC than on a forum.

To ensure variable name checking is enabled for all new modules, go to - Tools / Options / Editor (from the VBA Editor window) and set Require Variable Declaration to True (checked). For existing modules, ensure that the Option lines at the very top include :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
To compile your project, select (again from the VBA Editor window) Debug / Compile Project Name.

We ARE generally happy to help with compilation problems too (If you find an error reported and you can't resolve it, let us know), but we do expect members to have tried compiling before submitting a question. That way we have a better idea of the sort of problem we're looking at.

Welcome to Bytes!
Oct 8 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
4
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record...
5
by: Barn Yard | last post by:
good morning, im still kind of new to VBA but I have learned some interesting things so far. Right now I am normalizing my survey database so I'm having to run an append query for each...
9
by: Colin McGuire | last post by:
Hi, I have an report in Microsoft Access and it displays everything in the table. One column called "DECISION" in the table has either 1,2, or 3 in it. On my report it displays 1, 2, or 3. I want...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
2
by: NITIN MUNJAL | last post by:
I am trying to implement a simple way to authenticate users before entering my webpage. I have two textboxes one each for username and password. On the button click I call the function...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
3
by: cachaco87 | last post by:
Hi, I'm new to this boards, and I need some help finishing a project for school. Im using nested if-statements inside a while loop to execute different actions. action C consist of printing the...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
9
by: John | last post by:
Hello all. I am a PHP newbie and am having an issue using the && in an if statement. here is the code: if ($_REQUEST == "1" && date("Y-m-d") < $rowWork) { die("<h1>The earlybird special has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.