Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511

    Question Unanswered: Change Controls Caption Text From Array

    I have 20 option controls and I would like to set up the 20 option labels based on a previously selected option, and do the changes from an array. I have the array OK, but I get the error "can't find the name 'Me'" The option button labels are named oLb1, oLb2...oLb20. The option buttons are in a frame.

    Code:
    For i = 0 To 19
            cmdString = "Me.oLb" & i + 1 & ".Caption =" & aryStr(i)
            Eval (cmdString)
       Next i
    Jerry

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I suspect

    cmdString = "Me.oLb"

    should be

    cmdString = Me.oLb

    without the quotes.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    For i = 0 To 19
    cmdString = "Me.oLb" & i + 1 & ".Caption =" & aryStr(i)
    Eval (cmdString)
    Next i

    I usually use syntax like this...

    For i = 0 To 19
    cmdString = me.controls("oLb" & i + 1).Caption =" & aryStr(i)
    Eval (cmdString)
    Next i
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  4. #4
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511
    Thank you for both responses. It turns out that the Eval function is only used to return a value; it can not be used to change the caption of a control, as I attempted to do. Here is the partial code for the solution that I worked out, to change all of the option button label captions within a loop for 39 options inside of two frames.

    Code:
    Dim Ctl As Control
    Dim ctlName As String
    
    For i = 0 To 38
            ctlName = "oLb" & i + 1
            Set Ctl = Controls(ctlName)
            Controls(Ctl.Name).Caption = aryStr(i)
       Next i
    Access limits the number of option controls in a frame to 20, so I'll have to manage two frames with a total of 39 options to ensure that only one option is selected, by clearing a selected option in frame #1 if an item in frame #2 is selected, and visa versa.
    Why the odd number of 39 items? This is a picklist of the 39 books of the old testament.
    RESOLVED

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    This....
    For i = 0 To 19
    cmdString = me.controls("oLb" & i + 1).Caption =" & aryStr(i)
    Eval (cmdString)
    Next i

    is essentially the same thing as this...
    For i = 0 To 38
    ctlName = "oLb" & i + 1
    Set Ctl = Controls(ctlName)
    Controls(Ctl.Name).Caption = aryStr(i)
    Next i

    except that Controls(Ctl.Name).caption should (properly) be named me.controls(ctlName).caption = ..... (or me.controls(ctl.name).caption) = ...

    (Note: ctlName is the same as ctl.Name which is the same thing as ("oLb" & i + 1) as posted in my example.)

    Make sure your "i" value correctly corresponds to the controls. ie. if you don't have a control named oLb23, you'll get an error if cycling to 39. You are limited to a certain number of controls on the form but I can't recall the limit (it's a lot though.)
    Last edited by pkstormy; 02-27-10 at 01:49.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •