1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Access2k - How do I change the Label1.Caption Programatically?

Discussion in 'Business Applications' started by Bob Tech Man, Jan 26, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. Bob Tech Man

    Bob Tech Man Thread Starter

    Joined:
    Dec 30, 2005
    Messages:
    4
    I need to change several captions in labels. What I am trying is this:

    Dim mylabel, myform
    On Error GoTo trap

    MsgBox "Begin"

    For x = 397 To 399

    mylabel = "[Label" & Str$(x) & "]"

    mylabel.Caption = "test"

    MsgBox mylabel

    Next

    MsgBox "end"

    Exit Sub

    trap:
    Exit Sub

    This does not work. Any ideas on what would? Thanks
     
  2. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    There are some logical flaws in your code. Your variable mylabel is (I assume) intended to be of type Control, which is an object type. You must set a reference (or pointer) to the referent object in order to assign an object to an object variable:

    Code:
    Dim ctl As Control
    
    Set ctl = Me.Controls("Label397")
    
    ctl.Caption = "Some Caption"
    
    More importantly, you cannot refer to a variable, object, or declaration of any type via a string. In other words, this will not work:

    Code:
    Dim ctl As Control
    Dim str As String
    
    str = "Label397"
    
    Set ctl = Me.str
    
    ' or
    
    str.Caption = "Some Caption"
    
    In some cases, you may use the Eval function to evaluate an expression using user-defined procedures, but it may never be used to evaluate a string as a variable reference.

    To refer to a control using its name as a string, you need to use the Controls collection (like above).

    Code:
    Dim i As Integer
    
    For i = 397 To 399
      Me.Controls("Label" & i).Caption = "Some Caption"
    Next i
    
    I strongly suggest you give your controls more meaningful names. If you want to create a virtual collection of controls in Access, use their Tag properties.

    chris.
     
  3. Bob Tech Man

    Bob Tech Man Thread Starter

    Joined:
    Dec 30, 2005
    Messages:
    4
    Thanks, that hepled me to understand the syntax. What I have, and it works, is:
    ------------------------------------------------
    Dim x As Integer, y As Integer, mylabel As String, Mytime As Date

    For x = 349 To 360

    y = x - 348

    If y < 10 Then
    'MsgBox y & " - " & Me.Controls("MyOn0010_Label").Caption
    mylabel = Me.Controls("MyOn0" & y & "10_Label").Caption
    Me.Controls("Label" & x).Caption = mylabel
    Else
    mylabel = Me.Controls("MyOn" & y & "10_Label").Caption
    Me.Controls("Label" & x).Caption = mylabel
    End If

    Next
    ------------------------------

    Thanks a lot.
     
  4. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    I don't know what you're trying to do, but it's not very efficient if this is an ongoing procedural requirement. If you're interested in improving your routine, post some more details and perhaps we can help streamline the process.

    Otherwise, you may mark this thread Solved using the Thread Tools at the top of the page.

    chris.
     
  5. Bob Tech Man

    Bob Tech Man Thread Starter

    Joined:
    Dec 30, 2005
    Messages:
    4
    It is intended for me. All it does is change the captions of some labels. It gets the info to put in the label from labels to checkboxs that are used to mark status of a half hour block of time.

    Since the day is divided into half hour blocks there are a lot of checkboxs. The label.caption receiving the modified data is used to account for time zones.

    This will allow me to change the time according to time zones. I may pass this to friends in different time zones and want to have it ready to work there.

    Thanks for the insight.

    p.s. If you want a copy email me at [email protected]
    It is still only beta.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/437409

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice