Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image Control (xp)

    I have an image control (imgDice1) on a UserForm (frmDice). I am trying to load a picture when a certain condition is met triggered by a button click event. The code I am using is in a module but I am difficulty declaring and setting the appropriate variables. I am getting an object variable not set error or an object required error. Any takers?
    TIA.
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Image Control (xp)

    Not without seeing the code, preferably with the workbook and userform. Your question is kind of like my saying to you: The engine light is on in my car. Can you tell me what the problem is? <img src=/S/yep.gif border=0 alt=yep width=15 height=15>
    Legare Coleman

  3. #3
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Image Control (xp)

    Thanks for the response, Legare. Her is part of the code; I am getting an error indicating that imgDice1 is not set.

    Dim imgDice1 As Image
    Dim frmDice As UserForm
    Randomize
    x = Int(Rnd() * 6) + 1
    Randomize
    y = Int(Rnd() * 6) + 1
    Select Case x
    Case 1
    imgDice1.Picture = LoadPicture("Cocuments and Settingsetc")
    End Select

    Does this make things clearer?
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

  4. #4
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Image Control (xp)

    The code you posted does not set imgDice1 to anything. My best guess is that none of your code does either. Again, this is not much for us to go on to try to help you. My best guess is that you need a couple of statements somewhere in your code something like this:

    <code>
    Set frmDice = UserForm1
    Set imgDice1 = frmDice.Image1
    </code>
    Legare Coleman

  5. #5
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Image Control (xp)

    You do not have a line SETting the image, so it is not surprising that you get an error that it is not set.

    You declared the variable (DIM imgDice1) and then you tried to use it, but there is no image object yet. You must set it to something. Do you have an image object on the userform?

    Set imgDice1 = me.ImageObject1

    Steve

  6. #6
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Image Control (xp)

    Using your suggestion, I still get an object required error.

    Sub Roller()

    Dim imgDice1 As UserForm
    Dim frmDice As Image
    Set frmDice = UserForm1
    Set imgDice1 = frmDice.Image1
    Randomize
    x = Int(Rnd() * 6) + 1
    Randomize
    x = Int(Rnd() * 6) + 1
    Select Case x
    Case 1
    imgDice1.Picture = LoadPicture("Cocuments and SettingsAdministratorMy DocumentsWJECComputingDice PicsDie1.gif")
    End Select

    I'll try to be more explicit in my problem. I have 6 images (Die1.gif to Die6.gif) in a folder. They show each of the 6 faces of a day. When a random integer is generated between 1 and 6, I want the the relevant picture to be loaded into the image control named imgDice1 which is on a userform named frmDice. There is a button on the form which triggers a procedure called Roller located in Module1.

    If I place this code in the click event of the button, it works perfectly:

    Randomize
    x = Int(Rnd() * 6) + 1
    Randomize
    x = Int(Rnd() * 6) + 1
    Select Case x
    Case 1
    imgDice1.Picture = LoadPicture("Cocuments and SettingsAdministratorMy DocumentsetcDie1.gif")
    End Select
    End Sub

    However, I want the code to be located in a module and to be triggered by the button click. When I try to do so, I get the object required error or the object variable not set error. I hope this helps more and that I haven't muddied the water.
    Thanks for your time, Legare.
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

  7. #7
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Image Control (xp)

    That code is probably still getting the error because you did not change UserForm1 to the name of your actual form, and did not change Image1 to the actual name of the control on the form. This is exactly why I asked for you to upload the workbook with the form in my first response. We can sit here and guess what you have done in what you are not showing us for days. From the code that you have now shown us that you say does work, it looks like the form is named frmDice, and the image control is named imgDice1. In your code that you say does not work, it looks like you have also DIMed two variables with the same names. This is NOT a good idea. VBA can get confused when you do this. Continuing to guess at what is in what you have not shown us, this might be what you want:

    <code>
    Dim X As Integer, Y As Integer
    Randomize
    X = Int(Rnd() * 6) + 1
    Y = Int(Rnd() * 6) + 1
    frmDice.imgDice1.Picture = LoadPicture _
    ("Cocuments and SettingsAdministratorMy DocumentsWJECComputingDice PicsDie" & X & ".gif")
    frmDice.imgDice2.Picture = LoadPicture _
    ("Cocuments and SettingsAdministratorMy DocumentsWJECComputingDice PicsDie" & Y & ".gif")
    </code>
    Legare Coleman

Posting Permissions

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