Results 1 to 6 of 6

Thread: [RESOLVED] Object variable or With block variable not set (Error 91) Usercontrol

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [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
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object variable or With block variable not set (Error 91) Usercontrol

    Quote Originally Posted by DataMiser View Post
    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?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Object variable or With block variable not set (Error 91) Usercontrol

    Quote Originally Posted by Nightwalker83 View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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

  6. #6

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object variable or With block variable not set (Error 91) Usercontrol

    Quote Originally Posted by LaVolpe View Post
    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 View Post
    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!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width