How to highlight a txtbox background according to another table?

Robbie Doo 20 Reputation points
2025-11-17T17:14:14.0266667+00:00

I have a list of holidays in a table. I would like to use this list to highlight a holiday date on a calendar in Access.

Is there a language I can use?

Microsoft 365 and Office | Access | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Ken Sheridan 3,546 Reputation points
    2025-11-18T14:28:15.5933333+00:00

    Say you have tables named Calendar and Holidays, join the two tables in a query like this:

    SELECT CalendarDate, HolidayDate
    FROM Calendar LEFT JOIN Holidays
    ON Calendar.CalendarDate = Holidays.HolidayDate
    ORDER BY CalendarDate;
    

    You should then be able to use conditional formatting in a form or report based on the query, using the following expression:

    [CalendarDate] Is Not Null
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-17T17:15:30.58+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To highlight a textbox background in Access based on a list of holidays from a table, you can use VBA (Visual Basic for Applications). You would typically set up an event, such as the On Current event of the form, to check if the date in the textbox matches any date in your holidays table. If it does, you can change the background color of the textbox accordingly.

    Here’s a basic example of how you might implement this:

    1. Create a form that contains a textbox (e.g., txtDate) to display the date.
    2. Use the following VBA code in the form's code module:
         Private Sub Form_Current()
             Dim holidayDate As Date
             Dim rs As DAO.Recordset
             Set rs = CurrentDb.OpenRecordset("SELECT HolidayDate FROM Holidays")
         
             ' Reset background color
             Me.txtDate.BackColor = vbWhite
         
             ' Loop through the holidays
             Do While Not rs.EOF
                 holidayDate = rs!HolidayDate
                 If Me.txtDate.Value = holidayDate Then
                     Me.txtDate.BackColor = vbRed ' Change to your desired color
                     Exit Do
                 End If
                 rs.MoveNext
             Loop
             rs.Close
             Set rs = Nothing
         End Sub
      
    3. Adjust the SQL query to match the name of your holidays table and the field containing the holiday dates.

    This code checks each time the form is navigated to a new record and highlights the textbox if the date matches a holiday date from the table.

    Make sure to enable the DAO reference in your Access VBA environment to use the DAO.Recordset object.

    0 comments No comments

  2. Duane Hookom 26,555 Reputation points Volunteer Moderator
    2025-11-17T22:09:53.73+00:00

    What do you mean by “calendar in Access”? Is this the stock calendar control, a user built form, or a report?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.