object invalid or no longer set

Rob4077 used Ask the Experts™
I have a form with a few controls that affect the recordsource of a combo box on my form so I have set the On GotFocus event of the combo box to requery, which works fine. I then tried to extend the code to set the value to the first record in the underlying recordset if there's only one element in the recordset (see the code below). The requery runs fine but I get an "object invalid or no longer set" error in the line that follows the requery. Any suggestions as to what might be causing the error?
    If Me.cboDetailid.Recordset.RecordCount = 1 Then  '<<< the error occurs here
        Me.cboDetailid = CLng(Me.cboDetailid.ItemData(0))
    End If
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

 If Me.cboDetailid.listcount=0 then


Thanks for that. Problem solved!

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