Results 1 to 4 of 4
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Change the name of a control in code?

    Bummer! It took more than a few minutes to code a sub that would change the name of a label control only to discover Access doesn't allow that property to be modified. OR, AM I MISSING SOMETHING? (I previewed the collection, so know the label sequence was as needed.)


    Code:
    Private Sub reLabel()
    Dim C As Control
    Dim L As Integer
    Dim K As Integer
    Dim M As Integer
    Dim temp As String
    Dim bolStart As Boolean
    bolStart = False
    
    
    L = 11
    K = L
    M = 7
        For Each C In Me.Controls
            If TypeName(C) = "Label" Then
                If (C.Name = "label165") Or (bolStart = True) Then
                    bolStart = True
                    C.Name = "lblDrug" & K    <<<<<<<<<<<<<<<<< NO NO NO says access
                    Debug.Print C.Name
                K = K + 1
                M = M - 1
                    If M = 0 Then
                        M = 7
                        L = L + 10
                        K = L
                    End If
                End If
            End If
        Next C
        
    End Sub

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Why on earth would you want to do this via code? The name of a control is a fundamental property of that control that potentially propagates to queries, expressions and who knows what other references. Perhaps if the form/report was opened in design mode you could rename it, but that would require the code to be in a standard module, and your Me reference suggests it is not. In that case, it seems it would be a one-time thing which would make such code pointless in the first place. So what is the point of the exercise?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    As Micron mentions, code has to be in a standard module which opens the form in design view. Have the form call this PUBLIC code

    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Sub reLabel(fName As String)
    Dim C As Control
    Dim L As Integer
    Dim K As Integer
    Dim M As Integer
    Dim temp As String
    Dim bolStart As Boolean
    bolStart = False
    
    
    
    
    L = 11
    K = L
    M = 7
        DoCmd.OpenForm fName, acViewDesign, , , acNormal
        For Each C In Screen.ActiveForm.Section(acDetail).Controls
            If TypeName(C) = "Label" Then
                If (C.Name = "label165") Or (bolStart = True) Then
                    bolStart = True
                    C.Name = "lblDrug" & K    '<<<<<<<<<<<<<<<<< NO NO NO says access
                    Debug.Print C.Name
                K = K + 1
                M = M - 1
                    If M = 0 Then
                        M = 7
                        L = L + 10
                        K = L
                    End If
                End If
            End If
        Next C
        DoCmd.Close acForm, fName, acSaveYes
        DoCmd.OpenForm fName
    End Sub

  4. #4
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Thanks, a very different situation to be sure. Doing a retrofit and the code save me having to manually change the label names on 94 labels........... don't ask

Please reply to this thread with any new information or opinions.

Similar Threads

  1. code to change field size is not keeping change
    By markjkubicki in forum Programming
    Replies: 7
    Last Post: 04-04-2020, 01:28 PM
  2. Replies: 2
    Last Post: 04-28-2017, 10:21 AM
  3. Change top of each control
    By AmanKaur123 in forum Programming
    Replies: 8
    Last Post: 01-24-2017, 08:38 PM
  4. Replies: 1
    Last Post: 10-28-2014, 06:36 AM
  5. Replies: 2
    Last Post: 10-03-2014, 10:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums