Results 1 to 5 of 5
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250

    Dynamically send form names via VBA

    Hello,

    I want to automate text box design changes based on several conditions. For example, under some circumstances, I want to make a text box locked and change its appearance, so it's obvious that it's locked.

    I tried this:

    Code:
    Public Const COLOR_LOCKED As Long = 10855845        ' #A5A5A5
    
    
    Public Sub MakeControlLocked(FormName As String, ControlName As String, Optional LabelName As String)
    
        Forms!FormName!ControlName.ForeColor = COLOR_LOCKED
        Forms!FormName!ControlName.Locked = True
    
    End Sub
    And then call it from any form like this:



    Code:
    Private Sub cmdTest_Click()
    
        MakeControlLocked Me.Name, txtNotes
    
    End Sub
    However it seems like I cannot send form name as a parameter like this, because I get Runtime 2450: “Cannot Find The Referenced Form”

    Is there any way to accomplish this? Thanks.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Pass the form in as a Form, and the control as a control.

    Colin has some excellent examples on controls and controlling the Access UI
    https://isladogs.co.uk/greyed-out-controls/index.html
    https://isladogs.co.uk/set-controls/index.html
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Since it appears your only manipulating the controls, you only need to pass the control object

    Code:
    Public Sub MakeControlLocked(ctl as control)
    
       ctl.ForeColor = COLOR_LOCKED
       ctl.Locked = True
    
    End Sub
    calling code:
    Code:
    MakeControlLocked Me.YourControlName
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Now for why it didn't work: Forms! invokes reference to a collection of forms. What follows the bang (!) becomes the argument to whatever is the default member of the collection - in this case, the form name. You have no form by the name of "FormName" so it fails. IIRC, you can use a variable in place of a form name, but you would have to use the other syntax: Forms(FormName) or if the form name was frmMyForm, then Forms("frmMyForm").
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Thanks everyone for the insights, I have now made it work!

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

Similar Threads

  1. Dynamically creating form fields?
    By markcrobinson in forum Programming
    Replies: 2
    Last Post: 09-18-2020, 07:42 AM
  2. Dynamically Centering Form Fields
    By DarthBrute in forum Forms
    Replies: 8
    Last Post: 07-06-2019, 05:16 PM
  3. Replies: 3
    Last Post: 01-24-2014, 03:33 PM
  4. Form to dynamically run query with 'OR' clause
    By rhewitt in forum Programming
    Replies: 9
    Last Post: 10-24-2012, 01:24 PM
  5. Replies: 5
    Last Post: 04-24-2011, 03:14 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