Change label captions in Access VBA SEQUENTIALLY

Posted on 2011-10-12
Last Modified: 2012-08-13
I have a form with labels called lbl1, lbl2, lbl3 ....... lbl60

I want to write code that will refer to each label in turn and change the caption.

This code does NOT work:
Dim X as Integer

For X = 1 to 60
   Me.lbl & X & .Caption = "X"
Next X

I DON'T want to write 60 lines of code like this:
Me.lbl1.Caption = "1"
Me.lbl2.Caption = "2"
Me.lbl3.Caption = "3"
Me.lbl60.Caption = "60"

Any Ideas what I am doing wrong?


Question by:Biggles1
    LVL 47

    Accepted Solution

    me.controls("lbl" & X).Caption = X
    LVL 119

    Assisted Solution

    by:Rey Obrero

    use this

    For X = 1 to 60
       Me("lbl" & X) .Caption = X
    Next X
    LVL 61

    Assisted Solution

    The correct syntax has already been posted.

    <Any Ideas what I am doing wrong?>

    A couple of things.  

    1.  To use a control name as a variable, you need to build/define it as such.  To iterate through sequesntial conrol names, you need to append your loop variable to the "base string".  The beginning of the string needs to be in quotes since it is a constant value, not a variable.
          "lbl" & X
    The syntax in earlier posts allows you to use strings in quotes and/or variables:
          Me.Controls("lbl" & x)
    The syntax you initially posted will fall over with an error in part because Me.lbl is not a recognized control on your form.

    2.  = "X"

    The right hand side of your expression has issues too.  Even if the left hand side were syntactically ok, putting your loop variable in quotes like this turns it into a literal value.  Instead of showing a sequence, your 60 captions would all simply be "X".   Dropping the quottes allows the captions of your labels to be set to the value of your loop variable at each iteration.

    Author Closing Comment

    Thanks everyone.  Worked like a charm! (what was I thinking?)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    In the article entitled Working with Objects – Part 1 (, you learned the basics of working with objects, properties, methods, and events. In Work…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now