X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Referring To A Dynamic Control Name, Access 2016    
post Jul 15 2019, 11:56 AM

Posts: 648
Joined: 7-May 11

I have a large number of textboxes I want to apply this code to and was hoping to create a module call to this code. I thought by capturing the active control name and inserting it into the code that it would work. But I get an error that it cannot find the field strActiveCtl referred to in the expression.

Public Sub SelectText()
Dim strActiveCtl As String

strActiveCtl = "[Forms]![frm_Navigation]![NavigationSubform].Form!" & "" & Screen.ActiveControl.Name
Debug.Print strActiveCtl
    If Forms!frm_Navigation!NavigationSubform.Form!strActiveCtl.Value.SelLength = 0 Then     'Don't change selection if user has already selected text
        [Forms]![frm_Navigation]![NavigationSubform].Form!strActiveCtl.SelStart = 0
        [Forms]![frm_Navigation]![NavigationSubform].Form!strActiveCtl.SelLength = 9999
    End If
End Sub
Go to the top of the page
post Jul 15 2019, 12:10 PM

UA Admin
Posts: 35,516
Joined: 20-June 02
From: Newcastle, WA

Rather than try to identify the active control from inside the sub, I think you'll be ahead to pass the active control reference into the sub from wherever you call it.

Public Function SelectText(ctl as Control)  as Boolean

    Debug.Print ctl.Name
        SelectText = False
    If ctl.Value.SelLength = 0 Then     'Don't change selection if user has already selected text
         ctl.SelStart = 0
         ctl.SelLength = 9999
         SelectText= True
    End If
End Sub

My Real Name Is George. Grover Park Consulting is where I do business.
How to Ask a Good Question
Beginning SQL Server
Go to the top of the page
post Jul 15 2019, 12:16 PM

UtterAccess VIP
Posts: 6,773
Joined: 30-June 11

I'd create a function such as
Public Function SelCtrlText(ctl As Access.Control)
    If ctl.SelLength = 0 Then     'Don't change selection if user has already selected text
        ctl.SelStart = 0
        ctl.SelLength = 9999
    End If
End Function

And then simply call it
Call SelCtrlText(Me.YourControlName)

Call SelCtrlText([Forms]![frm_Navigation]![NavigationSubform].Form.Controls("YourControlName"))

Daniel Pineault (2010-2019 Microsoft MVP)
Professional Help: http://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: http://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc

All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
post Jul 15 2019, 01:09 PM

Posts: 2,541
Joined: 4-February 07
From: USA, Florida, Delray Beach

  1. Here is a slight variation of the Code that I created. For all those Text Boxes for which you may want the Code to apply to, enter 'Select' in their Tag Property. You don't need to supply an Argument but simply Call the Function and you also do not need to set the Focus to the Control.
  2. Sample Function Call and a display of the Return Value:
    Dim blnRet As Boolean

    blnRet = SelectText

    Debug.Print Screen.ActiveControl.Name, blnRet
  3. Function Definition:
    Public Function SelectText() As Boolean
    Dim ctl As Access.Control

    Set ctl = Screen.ActiveControl

    With ctl
      If .ControlType = acTextBox And .Tag = "Select" Then
       If .SelLength = 0 Then
         .SelStart = 0
         .SelLength = 9999
            SelectText = True
           SelectText = False
       End If
        SelectText = False
      End If
    End With
    End Function
  4. When the Function is executed, it:
    1. Checks to see if the ActiveControl is a Text Box with a Tag Property of 'Select'.
    2. If the above conditions are not met, then the Function returns False.
    3. Checks the SelLength of the ActiveControl and if a single character or more is selected, it returns False and Aborts.
    4. If nothing is selected in the TextBox, it selects the entire contents and returns True, assuming a is True.
  5. Hope this helps.
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    21st August 2019 - 09:49 AM