3

I'm using a technique similar to that in Remou's answer to this question to manipulate the propeties of the controls on a subform. Works great as long as the parent form's recordset has at least one record. If the parent form has no records, I get:

Error 2455, "You entered an expression that has an invalid reference to the property Form/Report."

The error is thrown when I attempt the recursive call. A simplified version of the code is below (I've stripped out error handling & a couple more Cases, including an Else). This code lives in a Module and is called from the Load event of most forms in my application:

Public Sub LockUnlockForm(frmLoad As Form)

Dim ctl As Control

    For Each ctl In frmLoad.Controls
        With ctl
            Select Case .ControlType
                Case acTextBox, acComboBox, acCheckBox
                    .Locked = Not gblnAuthorized
                Case acSubform
                    LockUnlockForm .Form  '<--- this line errors
            End Select
        End With
    Next

End Sub

What I want to do on the form that's giving me the problem right now is to create a new record at the parent level and allow the user to add data to the subform (if gblnAuthorized is True, or set a simple message if it's False). Do I have no choice but to do that before calling LockUnlockForm? Will creating a new parent-form-level record even work to prevent this error?

Community
  • 1
  • 1
RolandTumble
  • 4,633
  • 3
  • 32
  • 37
  • 6
    Determine the record count for the form's recordset and only run the For Each loop when the record count > 0. – HansUp Feb 17 '11 at 14:31
  • I have just tested with a parent form with no records, and it worked fine for me. I suspect that something else is happening that is interfering with the above code. What runs in your code if there are no records? – Fionnuala Feb 17 '11 at 20:17
  • @Remou: On button click in other form, it loads this form using the WhereCondition parameter of DoCmd.OpenForm. This code (in module) runs as first call from Form_Load. If this form has records, I'm golden, but if it has none I get indicated error & no controls show in Detail section. Testing for RecordCount=0 & skipping the call is working. – RolandTumble Feb 17 '11 at 22:27
  • 2
    Controls bound to the recordsource of the parent form (including subforms with LinkChild/LinkMaster properties) do not load when there are no records. – David-W-Fenton Feb 18 '11 at 03:05
  • @David-W-Fenton That's it! Thanks. I knew there was something but could not remember, all that was left in my head was the problem with setting visibility :( – Fionnuala Feb 18 '11 at 09:33
  • @David-W-Fenton: I don't think that's _quite_ right--or at least, the subform _control_ is still part of the Controls collection, even if its _form_ isn't loaded. Otherwise the Case wouldn't attempt the recursive call. Close enough for going on with, though. – RolandTumble Feb 18 '11 at 18:32

1 Answers1

13

Determine the record count for the form's recordset and only run the For Each loop when the record count > 0.

I'm answering this so other users with the same problem can easily determine the answer that was provided. And it's gone a month stale. Pass the credit to HansUp if you can!

Smandoli
  • 6,919
  • 3
  • 49
  • 83