Chuck0185
Chuck0185

Reputation: 531

Gray out disabled Controls in Access VBA after disabling the Control

I have an MS Access form with many tabs and subforms. I have written some VBA code for a button that disables/enables all of the controls on all of the subforms. For some reason the buttons will not gray out like they do if you manually set their Enabled property to False. Does anyone know how to achieve this with the VBA code?

Thank you!

Below is the code that I used:

Private Sub Command137_Click()
    Dim ctrlControl As Control
    For Each ctrlControl In Me.Controls
    On Error GoTo err:
    If ctrlControl.ControlType = acTabCtl Or ctrlControl.Name =     "Command9" Then

    Else
        ctrlControl.Enabled = False      
    End If
Continue:
Next
Exit Sub
err:
    Debug.Print err.Description
    Resume Continue
End Sub

Upvotes: 1

Views: 2561

Answers (2)

Jin Zhen
Jin Zhen

Reputation: 33

I ran into the same problem as well. However, I figured out a way to get around it.

first define some public variables in a module:

Option Explicit
Public greyout, white As Long

then in your Form_Load() sub

Public Sub Form_Load()
    'color RGB definition
    greyout = RGB(210, 210, 210)
    white = RGB(255, 255, 255)
End Sub

then when you use .enable on your text/combo boxes, do this

me.textbox.enabled = false
me.textbox.backcolor = greyout

me.textbox.enabled = true
me.textbox.backcolor = white

Hope it helps

Upvotes: 1

geeFlo
geeFlo

Reputation: 375

You're searching for the wrong controlType. Use 'acCommandButton' not 'acTabCtrl'. (I added an ELSE to enable everything else)

IF ctrlControl.ControlType = acCommandButton THEN
   ctrControl.enabled = false
ELSE
   ctrControl.enabled = true
END IF

Upvotes: 0

Related Questions