VBA Run-time Error 424 Object Require

Eileen Murphy
Eileen Murphy used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
BackColor value is Long
Check here
Eileen MurphyIndependent Application Developer

Author

Commented:
Even with a hex value?
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Eileen MurphyIndependent Application Developer

Author

Commented:
Screen Shot
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
Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Commented:
Eileen MurphyIndependent Application Developer

Author

Commented:
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"
Eileen MurphyIndependent Application Developer

Author

Commented:
***STUPID ME!!!! TYPO!!!

If ctrl.ControlType = acTextBox Then
                crtl.BackColor
Independent Application Developer
Commented:
Typo!
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
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).
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
***STUPID ME!!!! TYPO!!!
"Option Explicit" is the key.
You should add this at top of all your modules, and it should becomes a reflex.
Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Commented:
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!
Eileen MurphyIndependent Application Developer

Author

Commented:
Thanks all! Much appreciated!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial