Link to home
Start Free TrialLog in
Avatar of Eileen Murphy
Eileen MurphyFlag for United States of America

asked on

VBA Run-time Error 424 Object Require

VBA Object Required Error: Not sure why this isn't working. I've used this code in the past.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form_Current()

    If Me.NewRecord Then Exit Sub
   
    Me.CursorHold.SetFocus
   
    If Me.SelYear = "ALL" Then
        Call LockUnlockForm(True, "#D5D5D5")
        Me.lblLock.Visible = True
    Else
        Call LockUnlockForm(False, "#FFFFFF")
        Me.lblLock.Visible = False
    End If
   
End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function LockUnlockForm(ynEnabled As Boolean, strColor As String)

    If IsNull(Me.ProjectName) Then Exit Function
   
    Dim ctrl As Control
     
    For Each ctrl In Me.Controls
        If ctrl.Tag = "LockUnlock" Then
     
            If ctrl.ControlType = acTextBox Then
                crtl.BackColor = strColor <<<<<<<<<<<<<<<<<<<<< Run-time Error 424 Object Required
            End If
            ctrl.Locked = ynEnabled
        End If
    Next

End Function
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

BackColor value is Long
Check here
Avatar of Eileen Murphy

ASKER

Even with a hex value?
Even with a hex value?
Yep, an hexadecimal value is just a different representation of a number (base 16) than you usually see (base 10).
I suggest that you use the RGB() function when working with color, it is more readable and have more meaning than a pure number.
User generated image
Tried with long....

If Me.SelYear = "ALL" Then
        Call LockUnlockForm(True, RGB(213, 213, 213))
        Me.lblLock.Visible = True
    Else
        Call LockUnlockForm(False, RGB(255, 255, 255))
        Me.lblLock.Visible = False
    End If
Avatar of Daniel Pineault
Daniel Pineault

Thank you but it still bothers me.... the value on the long "color" is 14043909 and it is a text box - I have used this code before - in fact I copied it -- as I often do -- and it's got to be something stupid I'm missing...

Same error "Object Required"
***STUPID ME!!!! TYPO!!!

If ctrl.ControlType = acTextBox Then
                crtl.BackColor
ASKER CERTIFIED SOLUTION
Avatar of Eileen Murphy
Eileen Murphy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe use the Is operator ?
If (ctrl Is Access.TextBox) Then
    ctrl.BAckColor = lngColor
End If

Open in new window


Else drag & drop the Ctrl variable in the spy window, and check that it is an expected control (a textBox).
***STUPID ME!!!! TYPO!!!
"Option Explicit" is the key.
You should add this at top of all your modules, and it should becomes a reflex.
Glad you got it all sorted out!


Also, Fabrice is very right.  Try to remember to always add Option Explicit whenever you create a new module, it will save you many headaches in the long run!
Thanks all! Much appreciated!