1

I'm trying to return a reference to a checkbox, but the following code does not work as I expect.

Private Function ck() as Access.CheckBox
  ck = Me.ckExample
End Function

The full function takes parameters and has flow control to choose which control to return, but this is a minimal example to demonstrate the problem. This function errors out with the message

Run-item error '91': Object variable or With block variable not set

As far as I can tell it's attempting to set ck to the value of Me.ckExample rather than as a reference to the control, despite it being properly declared. I'm at a loss as to how to do this correctly. I've also attempted declaring it as Control, as well as assigning to a temporary variable declared with Dim ckTemp as Access.CheckBox.

Erik A
  • 31,639
  • 12
  • 42
  • 67
Myrddin Emrys
  • 42,126
  • 11
  • 38
  • 51
  • 5
    Since it's an object, you need to use `Set ck = Me.ckExample` – Erik A Feb 22 '18 at 18:17
  • If you posted more of the code we might be able to suggest a better solution to the wider problem – CallumDA Feb 22 '18 at 18:25
  • 1
    @CallumDA The purpose of a minimal example is to not distract from the root question with irrelevant details. Also why didn't you submit that as an answer @Erik? That's exactly what I was doing wrong. – Myrddin Emrys Feb 22 '18 at 18:33
  • On mobile, in meeting, hard to write an answer with proper formatting. Might do so later – Erik A Feb 22 '18 at 18:37
  • 1
    @MyrddinEmrys that's exactly what you're doing wrong, *syntactically*, sure. But since I cannot fathom a valid reason to have a `Private` function that returns a public member that's already on the interface, surely there's more to it. – Mathieu Guindon Feb 22 '18 at 18:45
  • @MyrddinEmrys, thank you for the explanation >.< . I've been here long enough to understand MCV. I offered to look at more code because it's not often you see functions written for this purpose, I get a hunch you've got a wider problem and we're falling into [xy territory](http://mywiki.wooledge.org/XyProblem). I was just offering to help. – CallumDA Feb 22 '18 at 18:46
  • 1
    Also @ErikvonAsmuth, if that's the only answer we get to on this question IMO it should be closed for basic syntax error rather than answered. – CallumDA Feb 22 '18 at 18:49
  • 1
    @CallumDA my mug approves this message. Let me dig through any one of the 800+ VBA questions that mention error 91.... – Mathieu Guindon Feb 22 '18 at 18:52
  • @Mat'sMug "The full function takes parameters and has flow control to choose which control to return, but this is a minimal example to demonstrate the problem." The control to return is dependant upon the data in the current record. – Myrddin Emrys Feb 22 '18 at 19:16
  • I spent an hour searching for various descriptions of the issue, but not the phrase `Run time error '91'`. While I agree solution is a duplicate, I think it's frustrating that the next person to search for 'how do I return a reference' in some context will once again run into a brick wall. – Myrddin Emrys Feb 22 '18 at 19:22
  • Well, that's where duplicates come in. As long as you don't delete, future users can find the answer more easily – Erik A Feb 22 '18 at 19:25

0 Answers0