Looping though controls

S

Stephen

I have a form that has about 30 buttons (setup as a restaurant menu to
select menu items). The challenge I am having is that the caption on each
button is different depending upon the master field. The challenge I am
having is rather simple though.

I have successfully setup a function that updates the caption on each button
with the appropriate caption. However the coding is rather long because it
has to specifically address EACH button. Hence I get over 30 lines of
repetitive code, with the only difference between lines being the control
name.

I am trying to create a more efficient loop that loops through the buttons,
but I can't get the syntax correct. Below is the code I am writing. Please
help (I have abbreviated some of the code to make it simplier for you to see
what I am doing)....

Function SetItemSelectionModifiers(GenItemID As Integer)

Dim db As DAO.Database
Dim strSQL As String
Dim strSelectItems As String
Dim rs As DAO.Recordset
Dim counter As Integer
Dim strPlaceString As String

Set db = CurrentDb

strSelectItems = "SELECT tblGenericItems.GenItemDescription " & _
"FROM tblGenericItems " & _
"ORDER BY tblGenericItems.GenItemDescription"

Set rs = db.OpenRecordset(strSelectItems)
counter = 0
While Not rs.EOF
counter = counter + 1
'------Set the strPlaceString as the control that is to be updated
strPlaceString = "Forms!frmModifierPopUp.SpecificModifierSelection." &
counter & ".Caption = rs!GenItemDescription"
'------- run the strPlaceString to update the control caption
strPlaceString
rs.MoveNext
Wend

End Function

I was unable to use "strPlaceString" on its own line even though it is a
correct syntax command. What would be the proper syntax to run
"strPlaceString"?

I tried:
docmd.runcommand....

Help...?
 
W

Wayne Morgan

strPlaceString is just a string variable, by itself it doesn't DO anything except hold a
string value. There is no ACTION that it will perform.

The action you are looking for should be performed by the equal sign in the string you are
assigning to this variable. Remove the variable and instead try this:

Forms!frmModifierPopUp.Controls("SpecificModifierSelection" & counter).Caption =
rs!GenItemDescription

This will perform the .Caption change on the control called

SpecificModifierSelection1
SpecificModifierSelection2
etc.
 
S

Stephen

Sandra -

This seems to almost work, however when the code runs, it is not going to
the correct lass on the subform. The subform has about 20 classes and when
the code finishes, it stays on the first class listed. Thoughts?

-Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top