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

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?
pcalabriaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
pcalabriaAuthor 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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

pcalabriaAuthor Commented:
On current.
Gustav BrockCIOCommented:
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.
PatHartmanCommented:
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.
pcalabriaAuthor 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???
Gustav BrockCIOCommented:
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.
PatHartmanCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.