Access Userform Conditional Formatting

Enchantix

New Member
Joined
Sep 20, 2015
Messages
34
Hi guys,

i need help to get the following to work...
Rich (BB code):
Private Sub Thickness_1_BeforeUpdate(Cancel As Integer)


If Me.Target_Thickness.Value = 2.6 Then

If Me.Thickness_1.Value < 1.5 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite


End If
End If

If Me.Target_Thickness.Value = 2.6 Then

If Me.Thickness_1.Value > 2.9 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite


ElseIf Me.Thickness_1 > 2.9 And Me.Thickness_1 < 1.5 Then

Me.Thickness_1.BackColor = vbGreen

Me.Thickness_1.ForeColor = vbBlue

End If
End If


End Sub
The part in Red doesnt want to work :(

Any ideas would be greatly appreciated

please and thanks
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
[COLOR=#0000FF]If Me.Thickness_1.Value > 2.9 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite[/COLOR]


[COLOR=#FF0000]ElseIf Me.Thickness_1 > 2.9 And Me.Thickness_1 < 1.5 Then[/COLOR]
A few things.

How can Me.Thickness_1 be greater than 2.9 AND less than 1.5 at the same time? That is not possible. Maybe you meant to use OR instead of AND?

Also, in the part of code I cut from your post above, the first IF is checking to see if Me.Thickness_1 is greater than 2.9. However, within that same IF statement, you have an ELSEIF (your first line in red), which is again, checking to see if Me.Thickness_1 is greater than 2.9 (and checking another thing).

The way an IF statement works, is if the initial IF statement is TRUE, it will not go to any ELSE clause under that IF statement. So you will never get to the red part.
 
Upvote 0
Code:
[COLOR=#0000FF]If Me.Thickness_1.Value > 2.9 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite[/COLOR]


[COLOR=#FF0000]ElseIf Me.Thickness_1 > 2.9 And Me.Thickness_1 < 1.5 Then[/COLOR]
A few things.

How can Me.Thickness_1 be greater than 2.9 AND less than 1.5 at the same time? That is not possible. Maybe you meant to use OR instead of AND?

Also, in the part of code I cut from your post above, the first IF is checking to see if Me.Thickness_1 is greater than 2.9. However, within that same IF statement, you have an ELSEIF (your first line in red), which is again, checking to see if Me.Thickness_1 is greater than 2.9 (and checking another thing).

The way an IF statement works, is if the initial IF statement is TRUE, it will not go to any ELSE clause under that IF statement. So you will never get to the red part.




Thank You for your response Joe,


Apologies for the rookie mistakes, im still learning.

its clear what the issue was.

I've managed to get it to work and id like to continue using code instead of the condtional formatting access has.



Private Sub Thickness_1_BeforeUpdate(Cancel As Integer)


Where: Me.Target_Thickness.Value = 2.6

If Me.Thickness_1.Value > 2.9 Or Me.Thickness_1.Value < 1.5 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite


ElseIf Me.Thickness_1.Value < 2.9 Or Me.Thickness_1.Value > 1.5 < 2.9 Then

Me.Thickness_1.BackColor = vbWhite

Me.Thickness_1.ForeColor = vbBlack

End If

End Sub


Now say if i was to add more conditions for Me.thickness_1 based on the value of Me.Target Thickness. How would i do that?

I.e.

Where: Me.Target_Thickness.Value = 3.2

If Me.Thickness_1.Value > 3.9 Or Me.Thickness_1.Value < 2.7 Then

Me.Thickness_1.BackColor = vbRed

Me.Thickness_1.ForeColor = vbWhite


ElseIf Me.Thickness_1.Value < 3.9 Or Me.Thickness_1.Value > 2.7 < 3.9 Then

Me.Thickness_1.BackColor = vbWhite

Me.Thickness_1.ForeColor = vbBlack

End If


Kind regards,
Ibbz
 
Upvote 0
Note that you can often use CASE statements in place of IF THEN (or in conjunction with them). See this links for details on this: MS Access: Case Statement. These are great when you are checking something, and it could be many different values that you want to all handle differently.

So I cleaned up your code a bit. You were having some issues with how you wrote the between checks. See if this works for you:
Code:
Private Sub Thickness_1_BeforeUpdate(Cancel As Integer)

    Select Case Me.Target_Thickness.Value
    
        Case 2.6
            If (Me.Thickness_1.Value > 2.9) Or (Me.Thickness_1.Value < 1.5) Then
                Me.Thickness_1.BackColor = vbRed
                Me.Thickness_1.ForeColor = vbWhite
            ElseIf (Me.Thickness_1.Value > 1.5) And (Me.Thickness_1.Value < 2.9) Then
                Me.Thickness_1.BackColor = vbWhite
                Me.Thickness_1.ForeColor = vbBlack
            End If

        Case 3.2
            If (Me.Thickness_1.Value > 3.9) Or (Me.Thickness_1.Value < 2.7) Then
                Me.Thickness_1.BackColor = vbRed
                Me.Thickness_1.ForeColor = vbWhite
            ElseIf (Me.Thickness_1.Value > 2.7) And (Me.Thickness_1.Value < 3.9) Then
                Me.Thickness_1.BackColor = vbWhite
                Me.Thickness_1.ForeColor = vbBlack
            End If
    
    End Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,821
Messages
6,162,157
Members
451,750
Latest member
pnkundalia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top