[RESOLVED] Object variable or With block variable not set (Error 91) Usercontrol
Hi,
I am moving some code of mine over to a user control but I am receiving "Object variable or With block variable not set (Error 91)" when I click the command button.
Code:
Public UserControl1 As UserControl1
Private Sub cmdRoll_Click()
UserControl1.Roll
End Sub
Private Sub Form_Load()
Set UserControl1 = UserControl1
End Sub
If I change the set line to
Code:
Set UserControl1 = New UserControl1
I receive "Invalid use of New keyword".
Why is this happening?
Thanks,
Nightwalker
Re: Object variable or With block variable not set (Error 91) Usercontrol
Why would you try to set an object to a new instance of an object and use the same name as the original. Even if that would work it would be rather silly and a total waste of code and cpu time.
Have you added the usercontrol to your form?
If so does it not have the name UserControl11 ? That would be the default.
Also if it is added then you do not need to dim it and you do not need to set it and if you did you would have to use a different name from the original.
For example you can
Code:
Dim txtB as TextBox
Set txtB=Text1
You can not
Code:
Dim TextBox as textbox
Set TextBox=TextBox
Also if you want to add a control at run time then you have to use the add method of the controls collection
Re: Object variable or With block variable not set (Error 91) Usercontrol
Quote:
Originally Posted by
DataMiser
If so does it not have the name UserControl11 ? That would be the default.
That was the problem! For some reason the default control had a name of "UserControl11" when I placed on the form even though double checking the name of the user control in the IDE it is "UserControl1". Removing the extra "1" solved the problem. Although, I still do not understand why it the default did not have the same name as the user control?
Re: Object variable or With block variable not set (Error 91) Usercontrol
Quote:
Originally Posted by
Nightwalker83
Although, I still do not understand why it the default did not have the same name as the user control?
Copy and paste 3 textboxes to a form and you get: Text1, Text2, Text3. So copy 3 Usercontrol1 and you get Usercontol11, Usercontol12, Usercontol13
Re: Object variable or With block variable not set (Error 91) Usercontrol
Yep the real problem is that you did not name your user control. By default it will be UserControl1 and when you add any control to a form it always takes its name and adds a number by default the first 1 being 1 so with a default user control you get UserControl11. This is normal behavior
Re: Object variable or With block variable not set (Error 91) Usercontrol
Quote:
Originally Posted by
LaVolpe
Copy and paste 3 textboxes to a form and you get: Text1, Text2, Text3. So copy 3 Usercontrol1 and you get Usercontol11, Usercontol12, Usercontol13
Quote:
Originally Posted by
DataMiser
Yep the real problem is that you did not name your user control. By default it will be UserControl1 and when you add any control to a form it always takes its name and adds a number by default the first 1 being 1 so with a default user control you get UserControl11. This is normal behavior
Thanks!