Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image.picture doen't work with a variable

Status
Not open for further replies.

BSHALL

Programmer
Jan 29, 2010
3
GB
The statement:
image100.picture = "c:\dir\picture.jpg"
works just fine.

BUT - when I try and assign the picture to a variable it fails:

str_pictureName = "c:\dir\picture.jpg"
image100.picture = str_pictureName

I get an error 2220 - Can't open file

Any suggestions?

Thanks
 
The correct syntax - look up Picture Property in Help - is:

object.Picture = LoadPicture( pathname )

So......the parameter of LoadPicture is the string.
Code:
str_pictureName = "c:\dir\picture.jpg"
image100.picture = LoadPicture(str_pictureName)

LoadPicture is the method that actually does the work.

You can see a hint that something else is going on behind the scenes by:
Code:
MsgBox Image1.Picture

' or image100.Picture
You will get something like:

738526989

In other words, NOT a string (path/filename), but a number.

Gerry
 
To remove a picture:
Code:
Image1.Picture = LoadPicture("")

BTW: I have not the foggiest what the number actually means, or even refers to.
Code:
Sub IntoPic()
Dim strImage As String

MsgBox Image1.Picture
   strImage = "c:\zzz\winter.jpg"
   Image1.Picture = LoadPicture("")
   Image1.Picture = LoadPicture(strImage)
MsgBox Image1.Picture
End Sub

Using the SAME image file, taking it and out and back in again, I get various numbers returned:

1527059080
1929712106

and one time.....

6546422788
-1694167384

Not a freakin' clue. In fact, if there is anyone who DOES know what these numbers mean, I would love to find out!

Gerry
 
I would like to know what "image100" is. Setting the picture property to a string makes no sense in any circumstance I can think of, as Gerry has already said

Gerry ... Image variables are Objects without default properties so simply return pointers to the image objects, addresses of whatever storage has been used.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Picture, on the other hand, does have a default property, which is Handle. This is documented as being an OLE_HANDLE, without being specific about what this means.

In reality it turns out to be the handle of the (GDI) bitmap (known in Microsoft's header files as an HBITMAP) that represents the picture. This can be handy if you need to resort to API access to the bitmap, which requires the HBITMAP.
 
Thanks everyone, but I am still getting the error 2220 cannot open file.

BTW, image100 is actually a form. What I am trying to do is change the background image on the form depending on the client who logs into the system.

Just can't seem to get it work as a variable, so I have resorted to a set of CASE statements, with the obvious downside that they need to be maintained in the source code now and can't be user maintained through a table!!!
 
Gerry has already provided you with a 'fix' - use LoadPicture

However, along with Tony and Gerry I can see no reason why

image100.picture = "c:\dir\picture.jpg"

"works fine". It shouldn't. It should generate an error (Type Mismatch).

However, using a string variable containing the file name should also raise a Type Mismatch error. And it isn't doing so, which - combined with the error number - suggests that there might be some custom error handling going on here. Or, perhaps, that image100 is not a Microsoft Forms 2 form.

 
Thanks again - Yes, it is an Access Form, and Gerry's fix doesn't work for me.

 
Ah - good point. Must stop making assumptions about what forms and applications someone might be using.

The problem is that in Access I don't see the error. Setting the Picture to a variable containing the filepath works absolutely fine on my rig (which I know isn't of much assistance)
 
FWIW, it works for me, too.

Can't open file, of course, isn't really a VBA error, and there may be all sorts of reasons for it. What they might be could be impossible to say from a distance, but perhaps if you could give precise detail of the steps you took, someone could help.

Just a thought - sometimes little things, like getting the casing of one of the characters in the file name wrong can cause odd problems like this.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
strongm said:
Ah - good point. Must stop making assumptions about what forms and applications someone might be using.
Heck, we did not even know it was a form (although "form" is still a hugely vague term)...never mind an Access form.


BSHALL, it is really helpful to state the relevant application and circumstances when posting.

I am still not even sure what image100 actually is. You state "form", but (not being an Access user) what does that mean. .Picture is usually a property of a control.

It can't be a userform, because a userform.Picture does require LoadPicture to insert a image into the .Picture property at run-time.

In other words:
Code:
image100.picture = "c:\dir\picture.jpg"
can NOT "work fine", as it returns (as strongm points out) a Type Mis-match error. So I too am interested in where - exactly - this seems to "work fine".

Also, as this seems to be a specific Access issue, you may want to post to the Access VBA forum.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top