Need help with syntax for using a variable to name a picture object on a form

pcalabria
pcalabria used Ask the Experts™
on
I am writing software that displays 1 to 5 stars on a form, and need to be able to programmatically refer to a picture object.. I can't figure out the syntax.

The number of stars is in an MS Access table that displays on the form.
The following code is in the on open event, but does not work

nn = Me.Stars

For pp = 1 To nn
   Forms("frmDetailListingPassives")("StarPix" & Trim(nn)).Picture = "C:\Disti-Master\Database\Star.jpg"
Next pp

Can anyone help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Are the 'stars' image controls on the form?

If they are try this.
For pp = 1 To nn
   Forms("frmDetailListingPassives").Controls("StarPix" & nn).Picture =LoadPicture( "C:\Disti-Master\Database\Star.jpg")
Next pp

Open in new window


PS The problem might actually have been no having LoadPicture.

Author

Commented:
Thanks Norie!  After I changed the nn to pp its "almost" working.
Here is the line that seems to work:
Forms("frmMainListingPassives").Controls("StarPix" & pp).Picture = "C:\Disti-Master\Database\Star.jpg"

Only problem I now have is that the form is bound to a table and I'm using continuous forms.  I was hoping for five forms to be displayed with a different number of stars... however only the first record (3 stars in my case) displays on all five continuous forms.

Any ideas?
NorieAnalyst Assistant

Commented:
Oops, didn't spot you weren't using the loop control variable.:)

Which event of the form are you using to run the code?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
On current.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
If it a continuous form, the picture control must be bound, or it will - for all records - always display the same picture.

An example - that does other things as well - can be found as a link in my article:

Show pictures directly from URLs in Access forms and reports
NorieAnalyst Assistant

Commented:
I don't know if it will work but instead of loading the images using the OnCurrent event why not have them all loaded at design time and then in the OnCurrent event hide/show the ones you want.
Distinguished Expert 2017

Commented:
That doesn't work Norie.  a continuous form is one form shown n times but only one set of properties is maintained.  That is why all unbound controls show the value of the current record.  You will notice as you click through the records, the image will change depending on what is needed for the currently Current record.

Author

Commented:
So I bound the form to a table and the table includes a field named stars.
At design time I populate the table with 1 to 5 stars.
I have a field called stars bound to the table and that field displays the correct number of starts as a number 1, 2, 3,4, or 5.

My goal is to display a graphic representation for the number of stars.
I hoped I could plant code somewhere in the form that would display the correct number of stars.
I can't get this to work??????


I hoped to use this code in the oncurrent of the form...

nn = Me.Stars

For pp = 1 To nn
   Forms("frmMainListingPassives").Controls("StarPix" & pp).Picture = "C:\Disti-Master\Database\Star.jpg"
Next pp


ALSO.. an extra form always displays... If I have five records in the table I get six forms, the last one is mostly blank. Can I stop that form from displaying???
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Again, if you wish different pictures on the records, the picture control must be bound - or they will all always show the same picture.
As demonstrated in my referenced demo.

The sixth record(?) you see, is probably the new record.
Set property AllowAdditions of the form to False to avoid that.
Distinguished Expert 2017

Commented:
Create a table to hold the StarsCount as the PK along with a picture.  As long as the starsCount value is in your original table, you can create a query that joins the original table to the new starsPictures table (use a Left Join to avoid a problem when the StarsCount is empty on the original table) and select the picture as the ControlSource for the bound image control.

No code is necessary.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial