Search code examples
ms-accesscasebackcolor

MS Access 2013 Change BackColor of Field if it contains string


I'm having trouble changing the color of my field when they containt certain words. Since the formatting rules are not working aswell i tried to write code.

Here's what i have:

Private Sub txtStatus_Exit(Cancel As Integer)
Select Case Me.Status
       Case "Verkauft"
       txtStatus.BackColor = vbRed
End Select
End Sub

The name of the field is txtStatus, actually it's a combination field with dropdown. What i doing wrong? I tried sever Actiontypes but it wont work :(


Solution

  • You should avoid combo fields by all means. They will cause you nothing but trouble as you've already seen.

    The actual value is probably a number, thus:

    Private Sub txtStatus_Exit(Cancel As Integer)
    
        Dim Status As Long
    
        Status = Me!txtStatus.Value
        ' Study value
        Debug.Print "Status:", Status
    
        Select Case Status
           Case 3    ' adjust to the value matching Verkauft. 
               Me!txtStatus.BackColor = vbRed
           Case Else
               Me!txtStatus.BackColor = vbWhite
        End Select
    
    End Sub