Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 16th, 2007, 08:51 PM
Authorized User
 
Join Date: Mar 2007
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



 
Old May 17th, 2007, 11:08 AM
Authorized User
 
Join Date: Mar 2007
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


 
Old May 25th, 2007, 07:10 AM
Friend of Wrox
 
Join Date: Jun 2003
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





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





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