Object variable not found with UserForm Image Control

Towelmonkey

New Member
Joined
Jan 12, 2022
Messages
7
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
I'm creating a tetris game in a modeless UserForm which aims to update Image Controls every gametick. However, I'm running into an issue where

"Object variable or With block variable not set"

is displayed every time I try to run the code (which is in a code module), and the error always points to lines of the form

formm.Designer.controls(controlName).Picture = LoadPicture("D:\square.bmp")

I'm using the VBA Extensibility library, and
Dim formm As VBComponent
formm = ....VBComponents.Items("UserForm1")
is delcared at the start of the module, followed by
UserForm1.Show vbModeless
before the block of problematic code.
There is nothing wrong with the spelling etc. of the string controlName, nor of the image file path.

I know that it is possible to load images into an already open UserForm with the use of user inputs like clicks and other buttons. My objective is to make the images load WITHOUT user input, on a time loop. Please advise on how to solve this error.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
FYI, running the problematic line of code individually in the Immediate window (while the userForm is not open) works, and the picture is loaded into the userForm template.
 
Upvote 0
I would suggest that you post your actual code so that if needed one can easily copy and paste it into a workbook for testing. Also, when posting your code, please use code tags to make it easier to read. It's the button just above the writing area that looks like </>.

Cheers!
 
Upvote 0
Here's the problematic sub:

VBA Code:
Sub testing()

Dim formm As VBComponent
Set formm = ThisWorkbook.VBProject.VBComponents.Items("UserForm1")
UserForm1.Show vbModeless
Debug.Print formm.Name   ' is printed
formm.Designer.Controls("imageCtrl1").Picture = LoadPicture("D:\square.bmp")    ' the error appears here
Debug.Print "Hi there!"    ' is not printed
End Sub

(The userform was already pre-populated with blank image controls.)
 
Upvote 0
You can't make design mode changes to a form while it's loaded. Why do you want to do that in code?
 
Upvote 0
I intend the UserForm to be the screen/graphical interface for my game (because using cells is too slow). Here are two videos on Youtube of other people's implementations. It looks like they were able to modify the pictures/colours of their form controls during runtime - I just need to know how.

 
Upvote 0
Use Userform1 instead of formm.designer for runtime changes.
 
Upvote 0
Omg please don't tell me that's it! I'll try it first thing tomorrow and update. Thanks for the help :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top