Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 16th, 2007, 08:51 PM
Authorized User
 
Join Date: Mar 2007
Location: Sydney, NSW, Australia.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing Fore colour and back colour

Hi All

I wish to change the font colour and the back colour for a field if a value is different to a corresponding field, here is my code, basically it is not working.

Private Sub Form_Current()
If Me.Fee.Value <> Me.SuggFee.Value Then
Me.Fee.BackColor = 255 And Me.Fee.ForeColor = 16777215

Else
Me.Fee.BackColor = -2147483643 And Me.Fee.ForeColor = -2147483640

End If

End Sub

Any Help would be great

Thanks
DB



Reply With Quote
  #2 (permalink)  
Old May 17th, 2007, 11:08 AM
Authorized User
 
Join Date: Mar 2007
Location: , , United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Darren

A simple syntax problem. Try this:-

Private Sub Form_Current()

    If Me.Fee.Value <> Me.SuggFee.Value Then
        Me.Fee.BackColor = 255
        Me.Fee.ForeColor = 16777215
    Else
        Me.Fee.BackColor = -2147483643
        Me.Fee.ForeColor = -2147483640
    End If

End Sub

The keyword "And" in VBA has a specific meaning, and does not match its usage in English, or in SQL. In VBA, it never means "do this as well", which is how you are using it here. It is used as a logical expression, testing whether two expressions both evaluate to Boolean True.

If I'm right about how Access was interpreting your code, the background colour was going black (value 0) - that is, (255 And (Me.Fee.ForeColor = 16777215)), or in English, if Forecolour is 16777215, then 255 (which counts as true) and true would be -1, but since you are setting that forecolour, then it is probably different, so you get True And False which is false, or 0, which is black.

Complicated, because you have effectively mixed integers and booleans; booleans have both a True/False value and a numeric (-1/0) value.

Hope that helps!

Richard


Reply With Quote
  #3 (permalink)  
Old May 25th, 2007, 07:10 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Actually, remember to code for equalities not inequalities if possible. It's standard practice and most programmers intuitively follow logic this way. Also, Value is not necessary.

If Me.Fee = Me.SuggFee Then
    Me.Fee.BackColor = -2147483643
    Me.Fee.ForeColor = -2147483640
Else
    Me.Fee.BackColor = 255
    Me.Fee.ForeColor = 16777215
End If

You may also skip all this and just place conditional formatting on the textbox.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Datagrid Colour based on the Value vuyiswamb VB.NET 2002/2003 Basics 0 May 22nd, 2008 02:24 AM
Changing a label's color (a.k.a. colour) ? Martin Woodhouse C# 2008 aka C# 3.0 7 May 8th, 2008 09:45 PM
Table row background colour changing meetravig Java GUI 0 September 21st, 2007 05:06 AM
pixel colour shanthi85 Beginning VB 6 0 May 22nd, 2007 11:23 AM
change the colour of some particular rows bimal ADO.NET 1 September 10th, 2004 02:31 AM



All times are GMT -4. The time now is 12:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.