Adding OptionButtons to array error 91

R

RB Smissaert

Have the following code to add OptionButtons to a control array:

A class called OptionButtonClass:

Option Explicit
Public WithEvents objOptionButton As MSForms.OptionButton

Private Sub objOptionButton_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)

Dim lContextHelpID As Long

If Button = 2 Then
On Error Resume Next
lContextHelpID = objOptionButton.HelpContextID
On Error GoTo 0
If lContextHelpID > 0 Then
ShowHelp bWebHelp, lContextHelpID
End If
End If

End Sub

In a normal module:

Public OptionButtons(24) As OptionButtonClass

Sub AddOptionButtonsToArray(oForm As UserForm, lStart)

Dim ctl As MSForms.Control
Dim i As Long

i = lStart

For Each ctl In oForm.Controls
If TypeName(ctl) = "OptionButton" Then
OptionButtons(i).objOptionButton = ctl
i = i + 1
End If
Next

End Sub

The above Sub is called like this:

AddOptionButtonsToArray MainForm, 0

I get an error 91: Object variable or with block not set when initializing
the form MainForm
and the line where the problem is is:
OptionButtons(i).objOptionButton = ctl

No idea why this falls over as exactly the same principle works fine with
checkboxes and labels.
Any advice with this greatly appreciated.


RBS
 
R

RB Smissaert

Thought I had found the mistake as it should be:

Set OptionButtons(i).objOptionButton = ctl

but still the same error.

RBS



RBS
 
R

RB Smissaert

Now forget about this. It was indeed a simple mistake.
It should be:
Public OptionButtons(24) As New OptionButtonClass
Just had left New out and all working fine now.

RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top