Error adding variables to old code (1 Viewer)

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:49
Joined
Jul 9, 2003
Messages
16,331
I don't like that you are using identical names for your objects as controls on the form. At best it's not good practice and at worst I think it could cause unforeseen issues. In any event it is advisable not to do it this way.

This is how I think your code should look:-

Code:
Private Sub cboPre_AfterUpdate()
Dim oCboPre As ComboBox
Dim oCboApple As ComboBox
Dim oCboOrange As ComboBox
Dim oCboGrapes As ComboBox

    Set oCboPre = Me!cboPre
    Set oCboApple = Me!cboApple
    Set oCboOrange = Me!cboOrange
    Set oCboGrapes = Me!cboGrapes

        If oCboPre.Value = "None" Then
            oCboApple.Enabled = False
            oCboOrange.Enabled = False
            oCboGrapes.Enabled = False
        Else
            oCboApple.Enabled = True
            oCboOrange.Enabled = True
            oCboGrapes.Enabled = True
        End If

End Sub


Note I have renamed the combobox objects to indicate that they are in fact objects...
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:49
Joined
Jul 9, 2003
Messages
16,331
Note that both "none" and "None" will evaluate as true.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:49
Joined
Jul 9, 2003
Messages
16,331
Please note that it would be much simpler if you access the controls directly without creating object variables to hold the Controls... Here is your code much simplified:-

Code:
Private Sub cboPre_AfterUpdate()
'Dim oCboPre As ComboBox
'Dim oCboApple As ComboBox
'Dim oCboOrange As ComboBox
'Dim oCboGrapes As ComboBox

        'Set oCboPre = Me!cboPre
        'Set oCboApple = Me!cboApple
        'Set oCboOrange = Me!cboOrange
        'Set oCboGrapes = Me!cboGrapes

                If cboPre.Value = "None" Then
                    cboApple.Enabled = False
                    cboOrange.Enabled = False
                    cboGrapes.Enabled = False
                Else
                    cboApple.Enabled = True
                    cboOrange.Enabled = True
                    cboGrapes.Enabled = True
                
                End If
End Sub
 

Adam Caramon

Registered User
Local time
Yesterday, 20:49
Joined
Jan 23, 2008
Messages
822
The goal is to grey out other controls (apple, orange and grapes) when "None" is selected for Control "cboPre"

Sometimes a simple example helps a lot - see attached database. 1 form, very simple. Make a selection for the first combobox and watch what happens in the second. Then take a look at the code.
 

Attachments

  • SimpleExample.accdb
    368 KB · Views: 42

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:49
Joined
Jul 9, 2003
Messages
16,331
If you were thinking of handling a lot of Controls then you might find an object oriented approach would be more useful. This approach could handle any number of controls up to the limit of the number of controls a form can contain, which I think is around 700...

Code:
Private Sub cboPre_AfterUpdate()

Dim Ctrl As Control

    For Each Ctrl In Me.Controls
        Select Case Ctrl.ControlType
            Case acComboBox ', acLabel, acListBox, acOptionButton, acOptionGroup, acTextBox, acToggleButton, acCheckBox
            If cboPre.Value = "None" Then
                If Ctrl.Name <> "cboPre" Then
                    Ctrl.Enabled = False
                End If
            Else
                Ctrl.Enabled = True
            End If
        End Select
    Next Ctrl
    
End Sub
 

Users who are viewing this thread

Top Bottom