Results 1 to 5 of 5
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Easy way to change background color for multiple fields

    I have a data entry form with many fields. I would like to have the fields with data colored and the empty ones white.



    I created the following code for Form_Current()

    If [Field1] <> "" Then
    [Field1].BackColor = RGB(255, 255, 204)
    Else
    [Field1].BackColor = RGB(255, 255, 255)
    End If

    If [Field2] <> "" Then
    [Field2].BackColor = RGB(255, 255, 204)
    Else
    [Field2].BackColor = RGB(255, 255, 255)
    End If

    I repeated the above for each field on the form.

    I have 2 questions

    1. Is there an easier way to accomplish this without repeating this for each field.
    2. After data is entered in a field and we tab to the next field, is there an easy way to have the previous field change color besides repeating the code on a lost focus or similar.

    Thanks,

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I would use Conditional Formatting, on the ribbon.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    I guess I was concerned that if this was opened on different pc's this may not always work where I figured VBA would work always. I also usually preferred code because it is easier to see when others try to analyze the database when I may not be available and conditional formatting is not visibly identified as being used. Thanks for the suggestion.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Up to you. You can create a function and call it from the current event plus all the after update events. It can look like your code, or use a loop of all controls and the tag property to identify which controls to test.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    you can simplify your if statements to one line per control which cuts down on the typing

    Field1.BackColor = RGB(255, 255, 204 - ((Nz(Field1, "") <> "") * 51))

    but I agree with Paul - conditional formatting is the way to go - will also work for a continuous form or datasheet which coding will not.

    if this was opened on different pc's this may not always work
    never had a problem myself

    conditional formatting is not visibly identified as being used
    put a note in the form module? It also appears in the documenter - for the control properties - although not what the rules actually are, but would serve as a warning for a later developer.

    You can also use the format property for a control - not the back colour but to display some text e.g.

    ;;;[Red]"This field must be completed"

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 07:38 AM
  2. Replies: 4
    Last Post: 10-28-2014, 09:51 AM
  3. Replies: 2
    Last Post: 06-01-2014, 12:05 PM
  4. Replies: 4
    Last Post: 05-04-2014, 08:24 PM
  5. Change Row Background Color Programmatically
    By sales@4bco.com in forum Programming
    Replies: 2
    Last Post: 10-25-2009, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums