Enabling/Disabling various checkboxes using different values withing the same field.

Tony

Registered User.
Local time
Today, 15:54
Joined
Oct 30, 2008
Messages
16
Request a little assistance. I’ve created a field within a form in MS Access that allows me to disable Check boxes when selecting various values within the same field. For example, within the field [Shop] there are three values; “ShopA”,” ShopB”, and” ShopC”. When selecting either of the fields it auto disables the checkbox128, checkbox 130 and so on…. respectively. (Works great!). However, I would like to also have “ShopC” within that same field, disable additional boxes that don’t necessarily apply to “ShopA” or “ShopB”. If you need additional information please reply. Thank you for addressing this issue.


Code as follows..


Sub Form_Current()
If (Me![Shop].Value = "ShopA") Or (Me![Shop].Value = "ShopB") Or (Me![Shop].Value = "ShopC")
Me!Check128.Enabled = False
Me!Check130.Enabled = False
Else
Me!Check128.Enabled = True
Me!Check130.Enabled = True

End If
End Sub
Private Sub Shop_AfterUpdate()
If (Me![Shop].Value = "ShopA") Or (Me![Shop].Value = "ShopB") Or (Me![Shop].Value = "ShopC")
Me!Check128.Enabled = False
Me!Check130.Enabled = False
Else
Me!Check128.Enabled = True
Me!Check130.Enabled = True

End If
End Sub
 
So much code to do so little.

Code:
Me!AdditionalBox.Enabled = (Me![Shop].Value = "ShopC")

Just put it either before or after your If statement.

A couple of things you should consider:
1. Drive this from a table.
2. Put all the logic for this in a single subroutine and call that subroutine from all the places you need to perform the logic.
3. Remove the If statements and use the syntax I've provided.

All of these will make this much easier to maintain. Any one of them will help.
 
Last edited:
Mr. Wilkinson,

First of all, thank you very much for your assistance. I do apologize about my lack of knowledge in MS Access. Please bare w/me this may take a little time and patience on your end.

I've applied your code just like you've stated both "before or after" the if statement and in both instances nothing occurred.

Your method is correct perhaps I'm not quite understanding the terminology or the correct placement of the code. A sample statement using your recommended code will help tremendously.

Much Appreciated.
 
Code:
Sub Form_Current()
Call SetControlStates
End Sub
Private Sub Shop_AfterUpdate()
Call SetControlStates
End Sub
 
Private Sub SetControlStates()
Dim bFirstState As Boolean
bFirstState = ((Me![Shop].Value = "ShopA") Or (Me![Shop].Value = "ShopB") Or (Me![Shop].Value = "ShopC"))
Me!Check128.Enabled = bFirstState 
Me!Check130.Enabled = bFirstState 
Me!AdditionalBox.Enabled = (Me![Shop].Value = "ShopC")
Me!YetAnotherAdditionalBox.Enabled = (Me![Shop].Value = "ShopC")
End Sub

Notice I moved all the logic into "SetControlStates". That is because the logic is identical when called from 2 places and now you can make changes in one place and it will apply to both places.

Also, the input to .Enabled is a boolean (True/False). The output of the = operator is also a boolean. Thus, you can set .Enabled = (1 = 1) 'True or .Enabled = (1 = 0) 'False. My earlier post was missing the parentheses that make VB use the = operator as an equivalency operator and not a set operator.

Table driven is not suitable for a 5 minute answer and is thus for a different day.
 
Mr. Wilkinson,

You are one GREAT American!!! It works well. Thank you for your help.

Respectfully,

Tony
 
Tony, as a followup to the PM you sent me and for everybody else's edification, there are several approaches you can take to dealing with null values in the text boxes.

Try this out:
Code:
Private Sub SetControlStates()
Dim bFirstState As Boolean
bFirstState = ((Me![Shop].Value & "" = "ShopA") Or (Me![Shop].Value & "" = "ShopB") Or (Me![Shop].Value & "" = "ShopC"))
Me!Check128.Enabled = bFirstState 
Me!Check130.Enabled = bFirstState
Me!AdditionalBox.Enabled = (Me![Shop].Value & "" = "ShopC")
Me!YetAnotherAdditionalBox.Enabled = (Me![Shop].Value & "" = "ShopC")
End Sub

This is essentially providing a value of "" to a text box that is null before doing the evaluation.
 

Users who are viewing this thread

Back
Top Bottom